Hashtable's Key Datatype

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Aads
    New Member
    • Mar 2008
    • 48

    Hashtable's Key Datatype

    Hi there,

    Please see the below code to understand my problem:

    Dim ht As Hashtable = New Hashtable
    With ht
    For indx As Short = 3 To 6
    .Add(indx, 3.1113213132)
    Next
    End With

    Now when I try to retrieve the value for a particular key (say for example: ht(5)), I get the value as "Nothing" but on the other hand if I change the datatype of the variable indx to Int32, it works absolutely fine.

    So my question is whether or not the hashtable support a key of type Short (Int16 in .NET Framework)?

    Regards,
    Aads
  • nukefusion
    Recognized Expert New Member
    • Mar 2008
    • 221

    #2
    My guess is that when you write ht(5), the compiler interprets the 5 as an Integer (Int32) and that is why it fails, as your keys are of type Int16.

    Take the following code for example. The first attempt to get the value will fail, the second will succeed.

    Code:
      Dim ht As Hashtable = New Hashtable
            With ht
                For indx As Short = 3 To 6
                    .Add(indx, 3.1113213132)
                Next
            End With
            Dim o As Object
    
            ' This attempt will fail
            o = ht(5)
    
            ' This attempt succeeds
            Dim s As Short = 5
            o = ht(s)

    Comment

    • Aads
      New Member
      • Mar 2008
      • 48

      #3
      Hi,

      Many thanks for letting me know the reason! I appreciate it.


      Cheers,
      Aads

      Comment

      Working...