Link to home
Start Free TrialLog in
Avatar of Chaffe
ChaffeFlag for Afghanistan

asked on

Access hashtable values

Greetings, In the following code, I build a hashtable using setHT() and I'm attempting to use GetHT() to gain access the value of the fields in the hashtable items, but I haven't been able to do so (ie: I'm trying to access the value of F2.Search)
What do I need to get to that value?  Thanks.

Public Class Fields
    Public FM As String
    Public Search As String
End Class
‘-----------------------------------------------------
Public Class work
Private m_FieldsSpecs As Hashtable

    Public ReadOnly Property FieldsSpecs() As Hashtable
        Get
            If m_FieldsSpecs Is Nothing Then SetFieldsSpecs()
            Return m_FieldsSpecs
        End Get
    End Property

Public Sub setHT()
        Dim f As New Fields

        f.FM = "FM1"
        f.Search = "Sea1"
        FieldsSpecs.Add("F2", f)

        f = New Fields
        f.FM = "FM2"
        f.Search = "Sea2"
        FieldsSpecs.Add("F2", f)
    End Sub

Public Sub GetHT()
        For Each var As DictionaryEntry In FieldsSpecs
            Type.GetType(var.Value.ToString).GetField("Searchable").GetValue(var.Value.ToString)  'This is not working.....
        Next
    End Function
End Class
Avatar of Chaffe
Chaffe
Flag of Afghanistan image

ASKER

Note: a typo in the GetHT function.
'-----------------------------------------
Public Sub GetHT()
        For Each var As DictionaryEntry In FieldsSpecs
            Type.GetType(var.Value.ToString).GetField("Search").GetValue(var.Value.ToString)  'This is not working.....
        Next
End Function
Avatar of cubixSoftware
cubixSoftware

Hi

To access the values via a DictionaryEntry

Public Sub GetHT()

        dim var as IDictionaryEnumerator = FieldsSpecs.GetEnumerator()
        While var.MoveNext
            messagebox.Show("My key is " & var.Key & " and my value is " & var.Value)
        Next

End Function
....

Hashtables work on Key/Value pairs so when you set values you must give it a Key and then a Value. In your setHT method you are using the same key "F2" so I am surprised your not getting some error here.

There seems to be a key you are using, ie FM1 and FM2 so you could use these as keys as follows

Public Sub setHT()
        Dim f As New Fields

        f.FM = "FM1"
        f.Search = "Sea1"
        FieldsSpecs.Add("FM1", f)

        f = New Fields
        f.FM = "FM2"
        f.Search = "Sea2"
        FieldsSpecs.Add("FM2", f)
    End Sub

or simply have an incrementing number

Public Sub setHT()
        Dim f As New Fields

        f.FM = "FM1"
        f.Search = "Sea1"
        FieldsSpecs.Add("1", f)

        f = New Fields
        f.FM = "FM2"
        f.Search = "Sea2"
        FieldsSpecs.Add("2", f)
    End Sub





HTH :)
Avatar of Chaffe

ASKER

cubixSoftware,  I acutally have 2 different keys in my code, this is just another typo in my example above.  I apologize.
in the function GetHT(), var.Value is giving me an object instance of the Fields class (with 2 fields in it and their values)
just to give you an example, when I loop in GetHT I get this in my first loop...

For Each var As DictionaryEntry In FieldsSpecs
    var.key     'This gives  "F1"
    var.value   'is getting me {Fields} and it has 2 fields in this object (FM & Serach and their values "FM1" & "Sea1")
Next

my goal here is to get a hold of the values of the object fields (FM & Search.)  Any thoughts on how to do this?  Thanks.
ASKER CERTIFIED SOLUTION
Avatar of cubixSoftware
cubixSoftware

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 Chaffe

ASKER

cubixSoftware, That did it.  Thanks for your help.