Re: comparing object values



On Wed, 27 Feb 2008 22:49:00 -0800, Vikram <Vikram@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

Thanks peter,
As per ur recommendation, I m thinking of going with approach 2. But is
there any way, by which I can force developer to implement Object.Equal for
any new property added later on. I mean, if later any new property is added,
of some user defined class type r of type string, then developer has to make
sure that he implemented equals for the new class or modify equals method for
any new property?

Yes, that's true. To some extent, that's just "business as usual". People updating or subclassing a particular class need to be aware of what it's already implementing. Note that even if you were using reflection for this particular purpose, that wouldn't obviate ths need. It would make that particular situation immune, but the class would otherwise still be susceptible.

That said, ironically this is an area where reflection _might_ be useful. As a maintenance aid, you might include some "#if DEBUG" code to the static constructor of the object that uses reflection to inspect the class's properties and the Equals() method. Unfortunately, I don't have the details as to how exactly you'd inspect the Equals() method, but I'm pretty sure there's a way to go through it and see if particular properties in the class are referenced in the method.

In that way, for debug builds, you could cross-reference the Equals() method implementation with the properties that actually exist and put up an assertion failure or throw an exception or whatever if it doesn't match. This would be an acceptable overhead in debug builds (it would only happen once per execution of the application), but would still address the maintenance issue.

You may not find it worth bothering with though. I know that this is not a generally used technique, and yet there are _lots_ of classes out there that override Equals(). Obviously the maintenance overhead and risks related to doing so are far outweighed by the benefits provided.

Pete
.



Relevant Pages

  • context of irp_mj_close
    ... Normally pid_close equals pid_create and A process id. ... But if I debug A in VC6 and terminate A with taskmgr, ... Does it mean irp_mj_close is in the context of another ... the context of this process object. ...
    (microsoft.public.development.device.drivers)
  • Re: Custom Function being called when it shouldnt.
    ... In both instances of the below the equals should be as I had changed it ... to equals to debug. ... if the active sheet isn't the sheet with the function ... > Exit Function ...
    (microsoft.public.excel.programming)

Loading