• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 547
  • Last Modified:

How to Extract a Structure Member Value from a Hash Table Collection Record

Given the simple code example below, could you please tell me how to extract
data from a Hash Table Collection in C#.
The question is described below in method findData().
THANKS VERY MUCH!!!

using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;

public partial class Form1 : Form
{
     Hashtable mCollectionOfTads = new Hashtable();
     ArrayList mAListColOfTads = new ArrayList();
     public struct mStoredStruct
     {
          public byte connectState;
          public UInt16 serialNum;      
          public byte healthState;
          public string partName;
     }

     public Form1()
     {
          InitializeComponent();
          addDataToHashTable();
     }
            
     private void addDataToHashTable()
     {
          // initially call this method to add two records to the hashTable
          mStoredStruct myTempStruct = new mStoredStruct();      
          // add structure to hashTable
          myTempStruct.connectState = 1;
          myTempStruct.serialNum = 1234;
          myTempStruct.healthState = 5;
          myTempStruct.partName = "bolt";
          string itemKey = myTempStruct.serialNum.ToString();
          mCollectionOfTads.Add(itemKey, myTempStruct);
          // add another structure to hashTable
          myTempStruct.connectState = 0;
          myTempStruct.serialNum = 5678;
          myTempStruct.healthState = 6;
          myTempStruct.partName = "nut";
          itemKey = myTempStruct.serialNum.ToString();
          mCollectionOfTads.Add(itemKey, myTempStruct);
     }
            
     private void findData(string myItem)      
     {            
               mStoredStruct myTempStruct = new mStoredStruct();                  
               myItem = "1234";            
               if ( mCollectionOfTads.ContainsKey(myItem) )                  
               {
                    // HELP
                    // how do I extract, for example, the
                    // value of myTempStruct.healthState or
                    // any other structure member from the
                    // hashTable record that has an item KEY
                    // of "1234"
     }
}

}
0
gmcorbe
Asked:
gmcorbe
  • 2
  • 2
1 Solution
 
pivarCommented:
Hi,

(( myTempStruct)mCollectionOfTads.Items["1234"]).healthState

/peter
0
 
pivarCommented:
Sorry, should read

((mStoredStruct)mCollectionOfTads.Items["1234"]).healthState
0
 
Fernando SotoCommented:
Hi gmcorbe;

This will do it.

private void findData(string myItem)
{
    mStoredStruct myTempStruct = new mStoredStruct();
    myItem = "1234";
    if (mCollectionOfTads.ContainsKey(myItem))
    {
        // To conserve on typing first get the structure from the HashTable
        // and cast it to its object type
        mStoredStruct mySS = ((mStoredStruct)mCollectionOfTads[myItem]);

        // Then use mySS to access all the fields
        byte connectState = mySS.connectState;
        UInt16 serialNum = mySS.serialNum;
        byte healthState = mySS.healthState;
        string partName = mySS.partName;
    }
}

Fernando
0
 
gmcorbeAuthor Commented:
It worked great.  THANKS VERY MUCH.
0
 
Fernando SotoCommented:
Not a problem, glad I was able to help.  ;=)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now