Link to home
Start Free TrialLog in
Avatar of gmcorbe
gmcorbeFlag for United States of America

asked on

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"
     }
}

}
Avatar of pivar
pivar
Flag of Sweden image

Hi,

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

/peter
Sorry, should read

((mStoredStruct)mCollectionOfTads.Items["1234"]).healthState
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gmcorbe

ASKER

It worked great.  THANKS VERY MUCH.
Not a problem, glad I was able to help.  ;=)