Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Using dictionaries

Using dictionaries

Scheduled Pinned Locked Moved C#
question
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Paul Harsent
    wrote on last edited by
    #1

    I have used a dictionary before in the form of Dictionary but is it valid to use it like this, Dictionary(TKey, TValue, TValue), if not what is the best way to do this or a good alternative. Thanks

    K 1 Reply Last reply
    0
    • P Paul Harsent

      I have used a dictionary before in the form of Dictionary but is it valid to use it like this, Dictionary(TKey, TValue, TValue), if not what is the best way to do this or a good alternative. Thanks

      K Offline
      K Offline
      Keith Barrow
      wrote on last edited by
      #2

      What you are asking for is syntactically illegal in a .net Dictionary. Formally a dictionary key directly maps onto the value (but not necessarily the reverse), and only one instance of key can exist per dictionary. What you need to do depends up what you are trying to acheive, if you give more information I might be able to provide more help.

      CCC solved so far: 2 (including a Hard One!)

      P 1 Reply Last reply
      0
      • K Keith Barrow

        What you are asking for is syntactically illegal in a .net Dictionary. Formally a dictionary key directly maps onto the value (but not necessarily the reverse), and only one instance of key can exist per dictionary. What you need to do depends up what you are trying to acheive, if you give more information I might be able to provide more help.

        CCC solved so far: 2 (including a Hard One!)

        P Offline
        P Offline
        Paul Harsent
        wrote on last edited by
        #3

        I am reading in a txt file, and pulling out a drive name(string), start time (float), end time (float) and maybe a date in the future but not using that just yet. These will need to be stored. There will be more than one start and end time per drive. Once the data has been read in and stored I will need to then read the data back to make a graph out of the data. Hope this is clear. Thanks

        P K 2 Replies Last reply
        0
        • P Paul Harsent

          I am reading in a txt file, and pulling out a drive name(string), start time (float), end time (float) and maybe a date in the future but not using that just yet. These will need to be stored. There will be more than one start and end time per drive. Once the data has been read in and stored I will need to then read the data back to make a graph out of the data. Hope this is clear. Thanks

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          You can use more complex types in a Dictionary, so you could have a Dictionary<string, List<float, float>> here.

          "WPF has many lovers. It's a veritable porn star!" - Josh Smith

          As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

          My blog | My articles | MoXAML PowerToys | Onyx

          1 Reply Last reply
          0
          • P Paul Harsent

            I am reading in a txt file, and pulling out a drive name(string), start time (float), end time (float) and maybe a date in the future but not using that just yet. These will need to be stored. There will be more than one start and end time per drive. Once the data has been read in and stored I will need to then read the data back to make a graph out of the data. Hope this is clear. Thanks

            K Offline
            K Offline
            Keith Barrow
            wrote on last edited by
            #5

            I think you need a class along the lines of

            public class RetrievedTiming
            {
            public string Drive { get; set; }
            public DateTime Start { get; set; }
            public DateTime End { get; set; }
            // public string Filepath { get; set; } //Possibly - see post
            }

            How you store this depends upon whether you only get one timing per file or not. If there is only one timing per file , You could use Dictionary<string, RetrievedTiming> where the string is the filename. If you have more than one timing per file the dictionary is no good, but you have a choice of either creating a class FileTimings which has the filename as a property and a List<RetrievedTiming> containing the timings from the file, or simply adding the commented Filepath property to the RetrievedTiming class and maintaining a list of this. [Edit] or, in line with Pete's Suggestion you could Have Dictionary<string, List<RetrievedTiming>> [/Edit] Personally, I'd start with the last option (the List<RetrievedTiming> with the Filepath property)and develop around that (and replace it with the FileTimings based option if it becomes unweildy). One of the good things about the last option is that you can use LINQ to search the timings easily. See here[^] for a very brief introduction to using LINQ to search a list of objects(in this case an array, but it applies to generic lists too).

            CCC solved so far: 2 (including a Hard One!)

            P 1 Reply Last reply
            0
            • K Keith Barrow

              I think you need a class along the lines of

              public class RetrievedTiming
              {
              public string Drive { get; set; }
              public DateTime Start { get; set; }
              public DateTime End { get; set; }
              // public string Filepath { get; set; } //Possibly - see post
              }

              How you store this depends upon whether you only get one timing per file or not. If there is only one timing per file , You could use Dictionary<string, RetrievedTiming> where the string is the filename. If you have more than one timing per file the dictionary is no good, but you have a choice of either creating a class FileTimings which has the filename as a property and a List<RetrievedTiming> containing the timings from the file, or simply adding the commented Filepath property to the RetrievedTiming class and maintaining a list of this. [Edit] or, in line with Pete's Suggestion you could Have Dictionary<string, List<RetrievedTiming>> [/Edit] Personally, I'd start with the last option (the List<RetrievedTiming> with the Filepath property)and develop around that (and replace it with the FileTimings based option if it becomes unweildy). One of the good things about the last option is that you can use LINQ to search the timings easily. See here[^] for a very brief introduction to using LINQ to search a list of objects(in this case an array, but it applies to generic lists too).

              CCC solved so far: 2 (including a Hard One!)

              P Offline
              P Offline
              Paul Harsent
              wrote on last edited by
              #6

              I have tried using Dictionary> = new Dictionary >(); but i get a compliation error using the generic type 'System.Collections.Generic.List' requires '1' type arguments Any ideas. Thanks

              K 1 Reply Last reply
              0
              • P Paul Harsent

                I have tried using Dictionary> = new Dictionary >(); but i get a compliation error using the generic type 'System.Collections.Generic.List' requires '1' type arguments Any ideas. Thanks

                K Offline
                K Offline
                Keith Barrow
                wrote on last edited by
                #7

                The syntax in your post is screwed up, if you want to display > you have to type & g t ; (without the spaces) and similar for < as Code Project renders like web page I can't help until I get the correct code, but it sounds like you are not declaring the generic properly. You can see examples in the previous posts.

                CCC solved so far: 2 (including a Hard One!)

                P 1 Reply Last reply
                0
                • K Keith Barrow

                  The syntax in your post is screwed up, if you want to display > you have to type & g t ; (without the spaces) and similar for < as Code Project renders like web page I can't help until I get the correct code, but it sounds like you are not declaring the generic properly. You can see examples in the previous posts.

                  CCC solved so far: 2 (including a Hard One!)

                  P Offline
                  P Offline
                  Paul Harsent
                  wrote on last edited by
                  #8

                  static Dictionary<string, List<float, float>> drive = new Dictionary<string, List<float, float>>(); this is the syntax im using. Thanks for the help

                  K 1 Reply Last reply
                  0
                  • P Paul Harsent

                    static Dictionary<string, List<float, float>> drive = new Dictionary<string, List<float, float>>(); this is the syntax im using. Thanks for the help

                    K Offline
                    K Offline
                    Keith Barrow
                    wrote on last edited by
                    #9

                    The nested generic syntax is very powerful, but ugly (but I think there is no rational way around that problem) so errors are often difficult to spot, especially if you haven't used them very much. The problem in the code snippet is List<float,float> you can only have one generic type in a list (e.g. List<float> If you want to store two floats this way, you need to create a class with two float properties (Like the example I posted yesterday) e.g.

                    public class Foo
                    {
                    public float Value1 { get; set; }
                    public float Value2 { get; set; }
                    }

                    public class Program
                    {
                    public void Main()
                    {
                    Dictionary<string, List<Foo>> Bar = new Dictionary<string, List<Foo>>();
                    }
                    }

                    I suggest you research both generics, and the various common collection types (e.g. List, Collection, Dictionary, Linked List, Hashtable, Set etc.). The .Net framework doesn't have all these (e.g. I've had to implement a Set type), but a grounding in each will let you know when to use it and will stand you in good sted in your career.

                    CCC solved so far: 2 (including a Hard One!)

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups