gmcorbe
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.ToS tring();
mCollectionOfTads.Add(item Key, myTempStruct);
// add another structure to hashTable
myTempStruct.connectState = 0;
myTempStruct.serialNum = 5678;
myTempStruct.healthState = 6;
myTempStruct.partName = "nut";
itemKey = myTempStruct.serialNum.ToS tring();
mCollectionOfTads.Add(item Key, myTempStruct);
}
private void findData(string myItem)
{
mStoredStruct myTempStruct = new mStoredStruct();
myItem = "1234";
if ( mCollectionOfTads.Contains Key(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"
}
}
}
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.ToS
mCollectionOfTads.Add(item
// add another structure to hashTable
myTempStruct.connectState = 0;
myTempStruct.serialNum = 5678;
myTempStruct.healthState = 6;
myTempStruct.partName = "nut";
itemKey = myTempStruct.serialNum.ToS
mCollectionOfTads.Add(item
}
private void findData(string myItem)
{
mStoredStruct myTempStruct = new mStoredStruct();
myItem = "1234";
if ( mCollectionOfTads.Contains
{
// 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"
}
}
}
Sorry, should read
((mStoredStruct)mCollectio nOfTads.It ems["1234" ]).healthS tate
((mStoredStruct)mCollectio
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It worked great. THANKS VERY MUCH.
Not a problem, glad I was able to help. ;=)
(( myTempStruct)mCollectionOf
/peter