barbary's Profile User Rating: -----

Reputation: 0 Apprentice
Group:
New Members
Active Posts:
13 (0.02 per day)
Joined:
13-August 12
Profile Views:
1,130
Last Active:
User is offline Nov 05 2012 02:44 AM
Currently:
Offline

Previous Fields

Country:
GB
OS Preference:
Windows
Favorite Browser:
Internet Explorer
Favorite Processor:
Intel
Favorite Gaming Platform:
PC
Your Car:
Who Cares
Dream Kudos:
0
Icon   barbary has not set their status

Posts I've Made

  1. In Topic: why i dont get exception ?

    Posted 29 Oct 2012

    I'll try to explain.

    The HashCode is not the key. The object is the key and the HashCode just helps the hashtable locate the value.

    So this is valid

    First obj1 = new First("mohamed");
    First obj2 = new First("Ali");
    Second Nobj = new Second(1);
    Hashtable ht = new Hashtable();
    ht[obj1] = Nobj;
    ht[obj2] = Nobj;
    ht.Add(new First("mohamed"), Nobj);
    ht.Add(new First("Ali"), Nobj);
    
    


    This is not

    First obj1 = new First("mohamed");
    First obj2 = new First("Ali");
    Second Nobj = new Second(1);
    Hashtable ht = new Hashtable();
    ht[obj1] = Nobj;
    ht[obj2] = Nobj;
    ht.Add(obj1, Nobj);
    ht.Add(obj2, Nobj);
    
    


    However adding this to First will make your orginal example throw an exception

    public override bool Equals(object obj)
    {
      return this.GetHashCode() == obj.GetHashCode();
    }
    
    


    However this probably just shows that your using hashtable in an inapproriate way, I don't know because you haven't said what your trying to do. Also Dictionary<T,T> is probably better than a hashtable for doing this.

My Information

Member Title:
New D.I.C Head
Age:
41 years old
Birthday:
June 18, 1973
Gender:
Years Programming:
20
Programming Languages:
c# .net

Contact Information

E-mail:
Click here to e-mail me

Friends

barbary hasn't added any friends yet.

Comments

barbary has no profile comments yet. Why not say hello?