Hello to every one !
What is the difference between a hashtable and a dictionary
secondly it says generally that dictionary is strongly typed while
hashtable is weakly typed...
what is mean by that .............
Printable View
Hello to every one !
What is the difference between a hashtable and a dictionary
secondly it says generally that dictionary is strongly typed while
hashtable is weakly typed...
what is mean by that .............
The Hashtable class and the Dictionary class perform the same function, i.e. storing values by key, where the keys are stored as hash values to facilitate fast searching. You can read up on the details of the implementation fi you like but suffice it to say that each key/value pair is stored and you can retrieve a value quickly by specifying the corresponding key.
In the case of the Hashtable, both the keys and the values can be any type, thus it is weakly-typed. There's no simple way for to enforce that either the keys or the values be any particular type, which can lead to issues. It also means that as you retrieve a value you must cast it as its actual type, because it will be returned via an Object reference.
The Dictionary class is implemented using .NET generics, and is actually the Dictionary(Of TKey, TValue) class. When you declare a Dictionary variable or create a Dictionary object you specify a type for the keys and the values. This ensures that only objects of those types can be added to the collection and it also means that the values will be returned as their own type, thus no casting is required, e.g.vb.net Code:
'Create a Dictionary that stores Integers values keyed on Strings. Dim d As New Dictionary(Of String, Integer) 'Add some key/value pairs. d.Add("John", 38) d.Add("Peter", 24) d.Add(35, "Mary") 'This would refuse to compile. Dim int As Integer = d("John")vb.net Code:
'Create a Hashtable that stores Integers values keyed on Strings. Dim h As New Hashtable 'Add some key/value pairs. d.Add("John", 38) d.Add("Peter", 24) d.Add(35, "Mary") 'This would compile,probably leading to issues at run time. Dim int As Integer = d("John") 'This would not compile with Option Strict On. Dim int2 As Integer = CInt(d("John")) 'The Object returned must be cast as its actual type.
Thanks for that code example, made things much clearer for me :thumb:
Is there a method of storing more than two values with the dictionary, as it will only accept two arguments?
Yes, store a class/struct/array.
How u an store multiple values in Collection data structure like u say
u can store class/struct
any coding example .............
Using a class:
Code:Class Employee
DateOfBirth As DateTime
JobTitle As String
End Class
' A hash table of employees by name:
Dim employees As New Dictionary(Of String, Employee)
If you want, you could also define generic structures/classes to hold specific numbers of values:
If you find yourself cobbling together otherwise disparate pairs of values often then this method could be more appropriate than defining a class to represent them.Code:Structure Bituple(Of T, U)
First As T
Second As U
End Structure
Dim things As Dictionary(Of Bituple(Of DateTime, String))
Thanks a lot all of u for ur assistance ...........
Specailly the code example are good..