Hashtable - storing hashtable objects inside a hashtable

Bonekrusher

Active member
Joined
Jul 4, 2007
Messages
38
Programming Experience
1-3
Hi,

I am trying to write some code which will allow me to store some values in a hashtable, then store that hashtable object in another hashtable.

I am able to create the hashtables, but I am having trouble retrieving the data.

VB.NET:
Public Class MyHash
    Public table As New Hashtable()
    Public Sub addHash(ByVal tablename As String, ByVal key As Object, ByVal value As String)

        If table.ContainsKey(tablename) Then
            Dim hashTable As New Hashtable()
            hashTable.Add(key, value)

        Else
            Dim hashTable1 As New Hashtable()
            hashTable1.Add(key, value)
            table.Add(tablename, hashTable1)
        End If

    End Sub

    Public Function getHash(ByVal tablename As String, ByVal key As Object)

        Dim value As String = ""
        If table.ContainsKey(tablename) Then
            Dim viewHash As New Hashtable()

            viewHash = table.Clone(tablename)

            If viewHash.ContainsKey(key) Then
                value = viewHash.Item(key).ToString()
            Else
                value = "null"
            End If

        End If
        For Each data As DictionaryEntry In table
            MsgBox(data.Key & "  " & data.Value.ToString)
        Next
        Return value
    End Function
End Class

My hashtable "table" holds the collection of hashtables. In function "getHash" I need to retrieve the hashtable from "tables" and then copy the hashtable to "viewHash". Once that has happened I can then retrieve some values by key. The problem (I think) is that I am not copying the hashtable correctly:
VB.NET:
viewHash = table.Clone(tablename)

I hope I explained my problem correctly.

Thanks for the help.
 
VB.NET:
If Not dTable.ContainsKey(tablename) Then dTable(tablename) = New ...
Didn't know that was possible, I thought dTable.Add was required... every day something new :)
 
They're actually subtly different:

dict(key) = value 'creates or overwrites existing
dict.Add(key, value) 'throw exception if existing
 
Back
Top