11 Replies - 1725 Views - Last Post: 30 October 2012 - 06:14 AM Rate Topic: -----

#1 ebousha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 29-May 12

why i dont get exception ?

Posted 12 October 2012 - 06:26 PM

class First
    {
        string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public First(string _name)
        {
            
            name = _name;
        }
        public override int GetHashCode()
        {
            return name.GetHashCode();
        }
    }
    class Second
    {
        int id;
        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        public Second(int _id)
        {
            id = _id;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            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);           
        }
    }

This post has been edited by tlhIn`toq: 12 October 2012 - 06:41 PM


Is This A Good Question/Topic? 0
  • +

Replies To: why i dont get exception ?

#2 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: why i dont get exception ?

Posted 12 October 2012 - 06:34 PM

What is your question?
What is supposed to happen?
What actually happens?
Are there errors? If any, paste them here.

Was This Post Helpful? 0
  • +
  • -

#3 ebousha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 29-May 12

Re: why i dont get exception ?

Posted 12 October 2012 - 06:40 PM

i have two value Similar ,hashtable dont allow enter two Reference similar but i override this option to name , if i enter two name similer i get exception
Was This Post Helpful? 0
  • +
  • -

#4 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: why i dont get exception ?

Posted 12 October 2012 - 07:34 PM

Quote

if i enter two name similer i get exception


Quote

Are there errors? If any, paste them here.

Was This Post Helpful? 0
  • +
  • -

#5 ebousha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 29-May 12

Re: why i dont get exception ?

Posted 13 October 2012 - 03:22 AM

yes
Was This Post Helpful? 0
  • +
  • -

#6 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: why i dont get exception ?

Posted 13 October 2012 - 07:04 AM

lol :lol:
Was This Post Helpful? 0
  • +
  • -

#7 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,279
  • Joined: 28-March 11

Re: why i dont get exception ?

Posted 13 October 2012 - 08:33 AM

@ebousha, can you be anymore vague? Don't just post code and expect everyone to know what your problem is. Post any errors your compiler is showing you. What are you expecting the outcome to be? What outcome are you getting? The more information you give, the better the help you receive!
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6049
  • View blog
  • Posts: 23,474
  • Joined: 23-August 08

Re: why i dont get exception ?

Posted 13 October 2012 - 09:32 AM

Perhaps you are not comfortable in the English language and find it difficult to relay to us what issues you are having. If that's the case you may have better luck in a forum in your native language.
Was This Post Helpful? 0
  • +
  • -

#9 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: why i dont get exception ?

Posted 13 October 2012 - 10:59 AM

I believe he's asking why doesn't he get an exception when he adds the third item to his Hashtable since he has an override on the GetHashCode for his class that uses 'name' as the source, and both objects he's adding have the same 'name' value.
Was This Post Helpful? 1
  • +
  • -

#10 ebousha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 29-May 12

Re: why i dont get exception ?

Posted 13 October 2012 - 11:58 AM

yes Momerath ,this is my problem any body can help me
Was This Post Helpful? 0
  • +
  • -

#11 barbary  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 13-August 12

Re: why i dont get exception ?

Posted 29 October 2012 - 09:23 AM

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.
Was This Post Helpful? 0
  • +
  • -

#12 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 181
  • View blog
  • Posts: 777
  • Joined: 27-October 11

Re: why i dont get exception ?

Posted 30 October 2012 - 06:14 AM

Please be more detailed in your explanation of your problem.

How did you expect the program to behave and how did it actually behave?

This post has been edited by RexGrammer: 30 October 2012 - 06:16 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1