• (cs) in reply to dubwai
    dubwai:
    pagefault:

    if ((object)string1 == (object)string2) // strings are the same identical instance

    Overloaded operators are not polymorphic, I take it.  The more I see how they are used, the more I'm glad I don't have to deal with them.

    How do you do it if you want to see if two Objects are equal polymorphically?  Is there an equals() method on object, like in Java?

    Correct, operators are not polymorphic.  I've heard of languages where they are, but at least in C decended languages it wouldn't make a lot of sense.  Reason being - how would you decide which of the parameters to do your virtual dispatch on?  The left side operand or the right?  Or both somehow?

    Operators are basically just static functions that take the two parameters as input.  You can make them behave polymorphically by calling a polymorphic function, if you like.  Then you can choose how the polymorphic method is dispatched.

  • Anonymous (unregistered)

    Simply gold. That's the worst WTF ever.

  • FVC (unregistered) in reply to Darren

    name is not the key, but the object he is looking for... you missed that one... ;)

  • WanFactory (unregistered) in reply to FVC

    OK, I'll play defender of the WTF. Perhaps the original coder wanted to return true in the case the hashtable contained an entry with key corresponding to the input parameter but the value associated with it was null.

  • (cs) in reply to WanFactory
    Anonymous:

    OK, I'll play defender of the WTF. Perhaps the original coder wanted to return true in the case the hashtable contained an entry with key corresponding to the input parameter but the value associated with it was null.

    Except it doesn't do that.   For that behavior, the line:

    if ((((string)tempHt[en.Key]) == name))

    would have to be changed to:

    if (en.Key == name))

  • Lucas (unregistered) in reply to nasch

    Anonymous:
    Polymorphic (in my understanding) would mean that the operation depends on the object or run-time type. The example you showed indicates that it depends on the reference or compile-time type. Then again, maybe we're thinking of different definitions of "polymorphic".

    You are correct in your definition of polymorphic. However, polymorphism determines the appropiate method to call at run-time depending on the run-time type of the instance of the object you are calling the method on. operator==( ) happens to be static, so there is no instance of an object and hence it isn't polymorphic. The objects to be compared are passed as arguments, as in operator==(string str1, string str2). You can see why operator==(object obj1, object obj2) is called instead in the case where 2 strings are compared through object references.

    At least that's the way I think it happens [;)]

     

  • Alan Balkany (unregistered)

    I've seen a relatively experienced programmer do the same thing.  The blame, in my opinion, goes to the Microsoft documentation of HashTable:  There is no LookUp-like method defined!  This is dysfunctional.

     

    After a frustrated programmer does a research project on the .NET hashtable, it turns out the LookUp function is implemented by a C# indexer: the [] brackets.  This doesn't appear in the Microsoft documentation, further reinforcing the notion that Microsoft documentation is often worthless.

  • (cs) in reply to Alan Balkany
    Anonymous:
    After a frustrated programmer does a research project on the .NET hashtable, it turns out the LookUp function is implemented by a C# indexer: the [] brackets.  This doesn't appear in the Microsoft documentation, further reinforcing the notion that Microsoft documentation is often worthless.

    From Microsoft's Documentation:

    Hashtable.Item Property

    Gets or sets the value associated with the specified key.

    [C#] In C#, this property is the indexer for the Hashtable class.

  • Adam Meltzer (unregistered)

    The least they could have done was break after found = true. Jeesh.

Leave a comment on “Not Too Hashish ... err ... Hashlike”

Log In or post as a guest

Replying to comment #:

« Return to Article