hashtable value array (late binding) error

false74

Well-known member
Joined
Aug 4, 2010
Messages
76
Programming Experience
Beginner
I have a hashtable that stores sports players: key = jersey number, value = an array of information
...
and what I am trying to do is put this information into combobox (but only showing a certain array elements), so for example say this was the information stored in the hashtable:
(key,{value})
"01",{"john smith","125lbs","qb"}
"02",{"john doe","205lbs","rb"}

so what I have done is created an arraylist that will store the items in the combobox.
but my problem happends when I try to loop through the array from the hashtable values.

(playerlisting is the hashtable)
VB.NET:
Dim playerlist As New ArrayList
        For Each item As DictionaryEntry In playerlisting
            Dim listitem As String = CStr(item.Key)

            For Each info As String In CType(item.Value, Array)
                listitem += info & " "
            Next

            playerlist.Add(listitem)
        Next
The above works, however since it's a foreach loop I cannot specify which array indexes I want to add onto the "listitem" string.

VB.NET:
            Dim arr As Array = CType(item.Value, Array)
            For i = 0 To arr.Length - 1
                listitem += arr(i) & " "
            Next
But, when I try a simple forloop instead I get a "late binding" error with the 3rd line. Even when I cast the arr(i) as a string I get the error. What needs to be changed? It's very misleading:confused:
 
Last edited:
Hmm...I've seen the late binding error before as well, but I was playing with a mess of Objects which were really collections...of collections, etc. The error turned out to be that I was trying to index into an Object without casting it down to a Collection type first.

There must be something funny with the Array datatype. Try declaring as String, since that appears to be your underlying datatype anyways.
Dim arr() as String = Ctype(item.Value, String())

Also, try adding the following lines to the top of your file. They should point the errors out when the program is compiled...but might generate a lot of other errors since this requires all typecasts be explicit.
Option Explicit On
Option Strict On
Option Infer Off
 
Replace ArrayList with Dictionary(Of String, String()). Saves you any type casting.
ignyte87 said:
Option Explicit On
Option Strict On
Option Infer Off
Set these options in Compile tab of project properties, or VB Defaults in Tools > Options > Projects & Solutions.
The default setting Infer On is much better to use, but I agree Option Strict turned On makes better sense.
 
Back
Top