Results 1 to 7 of 7

Thread: [RESOLVED] difference between a hashtable and a dictionary

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2007
    Location
    Karachi
    Posts
    551

    Resolved [RESOLVED] difference between a hashtable and a dictionary


    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 .............

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: difference between a hashtable and a dictionary

    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:
    1. 'Create a Dictionary that stores Integers values keyed on Strings.
    2. Dim d As New Dictionary(Of String, Integer)
    3.  
    4. 'Add some key/value pairs.
    5. d.Add("John", 38)
    6. d.Add("Peter", 24)
    7. d.Add(35, "Mary") 'This would refuse to compile.
    8.  
    9. Dim int As Integer = d("John")
    vb.net Code:
    1. 'Create a Hashtable that stores Integers values keyed on Strings.
    2. Dim h As New Hashtable
    3.  
    4. 'Add some key/value pairs.
    5. d.Add("John", 38)
    6. d.Add("Peter", 24)
    7. d.Add(35, "Mary") 'This would compile,probably leading to issues at run time.
    8.  
    9. Dim int As Integer = d("John") 'This would not compile with Option Strict On.
    10.  
    11. Dim int2 As Integer = CInt(d("John")) 'The Object returned must be cast as its actual type.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    UK
    Posts
    489

    Re: difference between a hashtable and a dictionary

    Thanks for that code example, made things much clearer for me

    Is there a method of storing more than two values with the dictionary, as it will only accept two arguments?
    Last edited by Bopo; Sep 21st, 2007 at 05:05 AM.
    Learning C♯

    Data Binding & Bound Controls - Objects and wizards will never be as intelligent as you, do it yourself! (Unless your Pro)

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: difference between a hashtable and a dictionary

    Yes, store a class/struct/array.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2007
    Location
    Karachi
    Posts
    551

    Re: difference between a hashtable and a dictionary

    How u an store multiple values in Collection data structure like u say
    u can store class/struct


    any coding example .............

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: difference between a hashtable and a dictionary

    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:
    Code:
    Structure Bituple(Of T, U)
      First As T
      Second As U
    End Structure
    
    Dim things As Dictionary(Of Bituple(Of DateTime, String))
    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.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2007
    Location
    Karachi
    Posts
    551

    Re: difference between a hashtable and a dictionary

    Thanks a lot all of u for ur assistance ...........
    Specailly the code example are good..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width