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

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
0
Chaffe
Asked:
Chaffe
  • 3
  • 3
1 Solution
 
ChaffeAuthor Commented:
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
0
 
cubixSoftwareCommented:
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
0
 
cubixSoftwareCommented:
....

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 :)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ChaffeAuthor Commented:
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.
0
 
cubixSoftwareCommented:
Can you try the following

dim field1 as string
dim field2 as string

field1 = ctype(var.value,Fields).FM
field2 = ctype(var.value,Fields).Search

0
 
ChaffeAuthor Commented:
cubixSoftware, That did it.  Thanks for your help.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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