We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

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

gmcorbe
gmcorbe asked
on
Medium Priority
558 Views
Last Modified: 2013-12-16
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"
     }
}

}
Comment
Watch Question

Commented:
Hi,

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

/peter

Commented:
Sorry, should read

((mStoredStruct)mCollectionOfTads.Items["1234"]).healthState
Retired
CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
gmcorbeSoftware Developer

Author

Commented:
It worked great.  THANKS VERY MUCH.
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
Not a problem, glad I was able to help.  ;=)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.