jackjohnson44
asked on
vb.net hashtable
I have a hashtable of hashtables.
For some reason I can't get to the inner hash table using a key.
For Each text As String In masterLoadedTests.Keys
Debug.Print(text)
Debug.Print(masterLoadedTe sts.Item(t ext).ToStr ing) 'this gives me a null reference
Next
I can doing it through enumeration
For Each x As Hashtable In masterLoadedTests.Values
Debug.Print(x.ToString) 'gives me System.Collections.Hashtab le
Next
For some reason I can't get to the inner hash table using a key.
For Each text As String In masterLoadedTests.Keys
Debug.Print(text)
Debug.Print(masterLoadedTe
Next
I can doing it through enumeration
For Each x As Hashtable In masterLoadedTests.Values
Debug.Print(x.ToString) 'gives me System.Collections.Hashtab
Next
Gotta ask... are you using VS2005?
ASKER
yes
1. Forget you ever heard of system.collections.... use memebrs of system.Collections.Generic ... always :)
2. Replace your hashtables with Dictionary(Of T1, T2) -- where T1 specifies your key type and T2 specifies your value type.
3.
Dim masterLoadedTests As New System.Collections.Generic .Dictionar y(Of String, Dictionary(Of String, String))
For Each key As String In masterLoadedTests.Keys
Dim thisDictionary As Dictionary(Of String, String) = masterLoadedTests(key)
For Each key2 As String In thisDictionary.Keys
Debug.Print(thisDictionary (key2))
Next
Next
End Sub
2. Replace your hashtables with Dictionary(Of T1, T2) -- where T1 specifies your key type and T2 specifies your value type.
3.
Dim masterLoadedTests As New System.Collections.Generic
For Each key As String In masterLoadedTests.Keys
Dim thisDictionary As Dictionary(Of String, String) = masterLoadedTests(key)
For Each key2 As String In thisDictionary.Keys
Debug.Print(thisDictionary
Next
Next
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I would rather use hash tables. I have a lot already working off this style.
Is there a difference?
Like I said above, I can enumerate through, I just can't call it explicitly
Debug.Print(masterLoadedTe sts.Item(t ext).ToStr ing) 'this gives me a null reference
look at the two examples above, each should work well.
Is there a difference?
Like I said above, I can enumerate through, I just can't call it explicitly
Debug.Print(masterLoadedTe
look at the two examples above, each should work well.
There's a huge difference.
1. Generics allow you to code faster.
2. Generics are type safe... you can't pass in variables that are of the wrong type.
3. Generics improve performance... you don't box and unbox to type Object.
1. Generics allow you to code faster.
2. Generics are type safe... you can't pass in variables that are of the wrong type.
3. Generics improve performance... you don't box and unbox to type Object.
ASKER
Why can't I get this to work.
I am not doing anything too crazy here.
Dim text1 As String = ""
For Each x As Hashtable In masterLoadedTests.Values
Debug.Print(x.ToString) 'prints System.Collections.Hashtab le which is correct
Next
For Each text As String In masterLoadedTests.Keys
Debug.Print(text)
Dim x As New Hashtable
x = masterLoadedTests.Item(tex t)
Debug.Print(x.ToString) 'gives me an error
text1 = text
Next
I am not doing anything too crazy here.
Dim text1 As String = ""
For Each x As Hashtable In masterLoadedTests.Values
Debug.Print(x.ToString) 'prints System.Collections.Hashtab
Next
For Each text As String In masterLoadedTests.Keys
Debug.Print(text)
Dim x As New Hashtable
x = masterLoadedTests.Item(tex
Debug.Print(x.ToString) 'gives me an error
text1 = text
Next
Any chance you have an empty hashtable as the value in that key?
ASKER
no,
I can loop through with a nested loop.
I can loop through with a nested loop.
ASKER
actually, this won't work
I get this error:
Unable to cast object of type 'System.Collections.Dictio naryEntry' to type 'System.Collections.Hashta ble'
For Each x As Hashtable In masterLoadedTests.Values
Debug.Print(x.ToString)
Next
For Each x As Hashtable In masterLoadedTests.Values
For Each y As Hashtable In x 'I get the error here
Debug.Print(y.ToString)
Next
Next
I get this error:
Unable to cast object of type 'System.Collections.Dictio
For Each x As Hashtable In masterLoadedTests.Values
Debug.Print(x.ToString)
Next
For Each x As Hashtable In masterLoadedTests.Values
For Each y As Hashtable In x 'I get the error here
Debug.Print(y.ToString)
Next
Next
How are you adding the Hashtable to the outer Hashtable?
Bob
Bob
ASKER
also, this does not work
masterLoadedTests.Add(sele ctedTestID , currentTest)
Debug.Print(selectedTestID .ToString) 'prints a number used as my key
Debug.Print(currentTest.To String) 'prints System.Collections.Hashtab le
Debug.Print(masterLoadedTe sts.Item(c urrentTest ).ToString ) 'I get a nullexception reference
masterLoadedTests.Add(sele
Debug.Print(selectedTestID
Debug.Print(currentTest.To
Debug.Print(masterLoadedTe
ASKER
sorry about that, typo it did work
error
Unable to cast object of type 'System.Collections.Dictio naryEntry' to type 'System.Collections.Hashta ble'.
see below
Debug.Print(selectedTestID .ToString)
Debug.Print(currentTest.To String)
Debug.Print(masterLoadedTe sts.Item(s electedTes tID).ToStr ing)
For Each x As Hashtable In masterLoadedTests.Values
For Each y As Hashtable In x 'error on this line
Debug.Print(y.ToString)
Next
Next
error
Unable to cast object of type 'System.Collections.Dictio
see below
Debug.Print(selectedTestID
Debug.Print(currentTest.To
Debug.Print(masterLoadedTe
For Each x As Hashtable In masterLoadedTests.Values
For Each y As Hashtable In x 'error on this line
Debug.Print(y.ToString)
Next
Next
can you not create a strongly typed collection of type hashtable inheriting collectionbase? Ive never tried it for hashtables but in theory you can create a strongly typed collection of any type.
Lojk... in VS2005 they're called generics :)
jackjohnson44
Did you try the code I posted earlier? If so, did it work?
Whilst I understand the points that Chaosian is making about alternatives, I am interested in why what you are doing is not working when code which, so far as I can see is identical in its essentials, is working for me. So if, but only if, the code that works for me also works for you it may be possible to work from that to identify the difference in the detail of the two approaches which is producing the problem.
Roger
Did you try the code I posted earlier? If so, did it work?
Whilst I understand the points that Chaosian is making about alternatives, I am interested in why what you are doing is not working when code which, so far as I can see is identical in its essentials, is working for me. So if, but only if, the code that works for me also works for you it may be possible to work from that to identify the difference in the detail of the two approaches which is producing the problem.
Roger
@chaosian
ahh.... picked up the technique in my VB6 days and vs2003 made it even easier to make 'Users' Collections of 'User' defined classes and ive always just gone that way (the MSDN said in vs2003 to inherit from CollectionBase). I will go away and have a look through the MSDN and do some research on Generics and their differences (are they any quicker or less memory hungry?).
Ive never had much reason to use hashtables..Why bother using a hashtable anyway if you can define your types (and collections of types) before you start? Is it a size/memory issue?
(collections of ) Collections of classes (and enums where relevant) let you do...
(where settings is a class of my creation containing various properties and public instances of class collections)
Settings.Users.Add("lojk")
Settings.Users("lojk").Pas sword ="Password"
Settings.Screen.Resolution =enXVGA
which helps to make the inline code so inherently readable, especially if you want to present any of those classes across an assembly boundary or to another developer. What would the equivalent code be using HashTables? The beauty of this is approach is Serialising and Deserialing these collections is simple and provides easily user-modifyable/readable (for example, configuration) files. (never write another 'GetSettings' sub again..)
OK so i know that my comment is a little off-topic (or is it? ;-)) so as for the original question...
Using 'text' as a variable name is not a great idea. isnt text a reserved word? (certainly is in my mind even if it isnt) and in some of the examples that work you 'x as hashtable in values' in some of the ones that dont you seem to use 'text as string in keys' and there doesnt seem to be much consitency..
also shouldnt this block say this?
Debug.Print(selectedTestID .ToString)
Debug.Print(currentTest.To String)
Debug.Print(masterLoadedTe sts.Item(s electedTes tID).ToStr ing)
For Each x As Hashtable In masterLoadedTests.Values
For Each y As Hashtable In x.values '<<<< or dictionaryentry or something else... whatever the enumerator is i suppose.
Debug.Print(y.ToString)
Next
Next
I dont really know-ive not tested any of the hashtable code on a compiler but i think a Strongly Typed Collection of Classes might be better in many ways.
ahh.... picked up the technique in my VB6 days and vs2003 made it even easier to make 'Users' Collections of 'User' defined classes and ive always just gone that way (the MSDN said in vs2003 to inherit from CollectionBase). I will go away and have a look through the MSDN and do some research on Generics and their differences (are they any quicker or less memory hungry?).
Ive never had much reason to use hashtables..Why bother using a hashtable anyway if you can define your types (and collections of types) before you start? Is it a size/memory issue?
(collections of ) Collections of classes (and enums where relevant) let you do...
(where settings is a class of my creation containing various properties and public instances of class collections)
Settings.Users.Add("lojk")
Settings.Users("lojk").Pas
Settings.Screen.Resolution
which helps to make the inline code so inherently readable, especially if you want to present any of those classes across an assembly boundary or to another developer. What would the equivalent code be using HashTables? The beauty of this is approach is Serialising and Deserialing these collections is simple and provides easily user-modifyable/readable (for example, configuration) files. (never write another 'GetSettings' sub again..)
OK so i know that my comment is a little off-topic (or is it? ;-)) so as for the original question...
Using 'text' as a variable name is not a great idea. isnt text a reserved word? (certainly is in my mind even if it isnt) and in some of the examples that work you 'x as hashtable in values' in some of the ones that dont you seem to use 'text as string in keys' and there doesnt seem to be much consitency..
also shouldnt this block say this?
Debug.Print(selectedTestID
Debug.Print(currentTest.To
Debug.Print(masterLoadedTe
For Each x As Hashtable In masterLoadedTests.Values
For Each y As Hashtable In x.values '<<<< or dictionaryentry or something else... whatever the enumerator is i suppose.
Debug.Print(y.ToString)
Next
Next
I dont really know-ive not tested any of the hashtable code on a compiler but i think a Strongly Typed Collection of Classes might be better in many ways.