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. Hashtable with Structure or Class Object

Hashtable with Structure or Class Object

Scheduled Pinned Locked Moved C#
tutorialquestion
12 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.
  • B Offline
    B Offline
    Bruce Coward
    wrote on last edited by
    #1

    I am trying to use a hashtable (or equivalent) to store and recover a number of small structures or classes. The hashtable record will need to have a UInst32 key and an associated data structure or class that looks some thing like this: bool bRecUsedFlag; UInt32 uiPGN; UInt32 uiRecCtr; byte[] bPGNData; I can see odd references that indicate that this is possible but every attempt I have to code it is failing. Can anyone give me a small example of how to create this table, add records to it and get records from it please? Many thanks, Bruce :)

    L 1 Reply Last reply
    0
    • B Bruce Coward

      I am trying to use a hashtable (or equivalent) to store and recover a number of small structures or classes. The hashtable record will need to have a UInst32 key and an associated data structure or class that looks some thing like this: bool bRecUsedFlag; UInt32 uiPGN; UInt32 uiRecCtr; byte[] bPGNData; I can see odd references that indicate that this is possible but every attempt I have to code it is failing. Can anyone give me a small example of how to create this table, add records to it and get records from it please? Many thanks, Bruce :)

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      First thing to do is design the class you want to contain your data, and its hash key. Then you just need to use the Hashtable[^] class to store and retrieve your individual objects. [edit]spelling[/edit]

      B 1 Reply Last reply
      0
      • L Lost User

        First thing to do is design the class you want to contain your data, and its hash key. Then you just need to use the Hashtable[^] class to store and retrieve your individual objects. [edit]spelling[/edit]

        B Offline
        B Offline
        Bruce Coward
        wrote on last edited by
        #3

        That is what I thought was possible but I must be missing a trick because everyway I come at this I hit problems that I haven't solved yet. Can anyone point this comparative newbie in the right direction with a simple example based on the data in my first email please? Many thanks, Bruce

        L 1 Reply Last reply
        0
        • B Bruce Coward

          That is what I thought was possible but I must be missing a trick because everyway I come at this I hit problems that I haven't solved yet. Can anyone point this comparative newbie in the right direction with a simple example based on the data in my first email please? Many thanks, Bruce

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Bruce Coward wrote:

          Can anyone point this comparative newbie in the right direction with a simple example based on the data in my first email please?

          I just did so; did you look at the examples in that link?

          B 1 Reply Last reply
          0
          • L Lost User

            Bruce Coward wrote:

            Can anyone point this comparative newbie in the right direction with a simple example based on the data in my first email please?

            I just did so; did you look at the examples in that link?

            B Offline
            B Offline
            Bruce Coward
            wrote on last edited by
            #5

            I looked at those examples but all I could see were bog standard string keys and single string data. This I have working. My problem is lifting the data up into a structure or class... Cheers Bruce

            L 1 Reply Last reply
            0
            • B Bruce Coward

              I looked at those examples but all I could see were bog standard string keys and single string data. This I have working. My problem is lifting the data up into a structure or class... Cheers Bruce

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Bruce Coward wrote:

              My problem is lifting the data up into a structure or class

              It's just the same, instead of String you use your own class. Here[^] is the top link Google offers, you can work through this tutorial to get an understanding of Hashtable basics, and then use the same code with your custom class as the content. Remember that a string is just a class in C#.

              B 1 Reply Last reply
              0
              • L Lost User

                Bruce Coward wrote:

                My problem is lifting the data up into a structure or class

                It's just the same, instead of String you use your own class. Here[^] is the top link Google offers, you can work through this tutorial to get an understanding of Hashtable basics, and then use the same code with your custom class as the content. Remember that a string is just a class in C#.

                B Offline
                B Offline
                Bruce Coward
                wrote on last edited by
                #7

                Hi Richard, Thank you for your continuing patience... I have spent quite a time with that link yesterday and am happy with it's basic message. I appreciate that string is only a class but my problem comes when I try to build my small class and store it in the Hashtable. I can do that and by single stepping I can see my two data records be added to the hashtable. I notice that the hashtable seems to add both records but put the second records data over the first hash position as well as the second hash posotion but I haven't chased that problem yet. My real problem comes when I try to recover the records when I am not getting the data back. Here is my code:

                namespace ConsoleApplication7
                {
                class Program
                {
                class csFPRec
                {
                public UInt32 uiPGN;
                public UInt32 uiFrameCtr;
                }

                    static void Main(string\[\] args)
                    {
                        // Instantiate Class
                        csFPRec FPRec = new csFPRec();
                        Hashtable HT = new Hashtable();
                
                        Console.WriteLine("Writing Initial Records");
                        
                        Console.WriteLine("Writing 0x1234");
                        FPRec.uiPGN = 0x1234;
                        FPRec.uiFrameCtr = 0x01;
                        HT.Add(FPRec.uiPGN, FPRec);
                        
                        Console.WriteLine("Writing 0x5678");
                        FPRec.uiPGN = 0x5678;
                        FPRec.uiFrameCtr = 0x02;
                        HT.Add(FPRec.uiPGN, FPRec);
                        
                        Console.WriteLine();
                        Console.WriteLine("Reading Records");
                
                        // Get record back from Hashtable
                        object pgnobj = new object();
                        pgnobj = HT\[0x1234\];
                
                
                        Console.ReadLine();
                    }
                }
                

                }

                As you can tell I am a newbie with C# having been writing embedded C for more than 20 years. Best regards

                E 1 Reply Last reply
                0
                • B Bruce Coward

                  Hi Richard, Thank you for your continuing patience... I have spent quite a time with that link yesterday and am happy with it's basic message. I appreciate that string is only a class but my problem comes when I try to build my small class and store it in the Hashtable. I can do that and by single stepping I can see my two data records be added to the hashtable. I notice that the hashtable seems to add both records but put the second records data over the first hash position as well as the second hash posotion but I haven't chased that problem yet. My real problem comes when I try to recover the records when I am not getting the data back. Here is my code:

                  namespace ConsoleApplication7
                  {
                  class Program
                  {
                  class csFPRec
                  {
                  public UInt32 uiPGN;
                  public UInt32 uiFrameCtr;
                  }

                      static void Main(string\[\] args)
                      {
                          // Instantiate Class
                          csFPRec FPRec = new csFPRec();
                          Hashtable HT = new Hashtable();
                  
                          Console.WriteLine("Writing Initial Records");
                          
                          Console.WriteLine("Writing 0x1234");
                          FPRec.uiPGN = 0x1234;
                          FPRec.uiFrameCtr = 0x01;
                          HT.Add(FPRec.uiPGN, FPRec);
                          
                          Console.WriteLine("Writing 0x5678");
                          FPRec.uiPGN = 0x5678;
                          FPRec.uiFrameCtr = 0x02;
                          HT.Add(FPRec.uiPGN, FPRec);
                          
                          Console.WriteLine();
                          Console.WriteLine("Reading Records");
                  
                          // Get record back from Hashtable
                          object pgnobj = new object();
                          pgnobj = HT\[0x1234\];
                  
                  
                          Console.ReadLine();
                      }
                  }
                  

                  }

                  As you can tell I am a newbie with C# having been writing embedded C for more than 20 years. Best regards

                  E Offline
                  E Offline
                  Estys
                  wrote on last edited by
                  #8

                  Try this :

                          ....
                          FPRec = new csFPRec();
                          FPRec.uiPGN = 0x5678;
                          FPRec.uiFrameCtr = 0x02;
                          ...
                  

                  You added the same instance FPRec twice

                  B 1 Reply Last reply
                  0
                  • E Estys

                    Try this :

                            ....
                            FPRec = new csFPRec();
                            FPRec.uiPGN = 0x5678;
                            FPRec.uiFrameCtr = 0x02;
                            ...
                    

                    You added the same instance FPRec twice

                    B Offline
                    B Offline
                    Bruce Coward
                    wrote on last edited by
                    #9

                    Thanks Richard - that has fixed the records being the same on the hashtable. Can you see what I am doing wrong in getting the records off the hashtable? Many thanks, Bruce :thumbsup:

                    B 1 Reply Last reply
                    0
                    • B Bruce Coward

                      Thanks Richard - that has fixed the records being the same on the hashtable. Can you see what I am doing wrong in getting the records off the hashtable? Many thanks, Bruce :thumbsup:

                      B Offline
                      B Offline
                      Bruce Coward
                      wrote on last edited by
                      #10

                      Woops! - Wrong thanks. Thanks Estys. Chers Bruce :thumbsup:

                      E 1 Reply Last reply
                      0
                      • B Bruce Coward

                        Woops! - Wrong thanks. Thanks Estys. Chers Bruce :thumbsup:

                        E Offline
                        E Offline
                        Estys
                        wrote on last edited by
                        #11

                        ...
                        pgnobj = HT[(UInt32)0x1234];
                        ...

                        The basic Comparer is Object.Equals() Your key value was converted (cast) to System.Int32 which does NOT equal UInt32 (I suspect)

                        modified on Wednesday, December 16, 2009 9:21 AM

                        B 1 Reply Last reply
                        0
                        • E Estys

                          ...
                          pgnobj = HT[(UInt32)0x1234];
                          ...

                          The basic Comparer is Object.Equals() Your key value was converted (cast) to System.Int32 which does NOT equal UInt32 (I suspect)

                          modified on Wednesday, December 16, 2009 9:21 AM

                          B Offline
                          B Offline
                          Bruce Coward
                          wrote on last edited by
                          #12

                          Got It! Many thanks Estys. Solved it. Really appreciate your help. Cheers Bruce :)

                          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