Hashtable with Structure or Class Object
-
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 :)
-
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 :)
-
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
-
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
-
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?
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
-
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
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#.
-
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#.
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
-
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
-
Try this :
.... FPRec = new csFPRec(); FPRec.uiPGN = 0x5678; FPRec.uiFrameCtr = 0x02; ...
You added the same instance FPRec twice
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:
-
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:
Woops! - Wrong thanks. Thanks Estys. Chers Bruce :thumbsup:
-
Woops! - Wrong thanks. Thanks Estys. Chers Bruce :thumbsup:
-
...
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
Got It! Many thanks Estys. Solved it. Really appreciate your help. Cheers Bruce :)