operator== overloading and null check problem

by volkanuzun 3/16/2008 7:44:00 AM

For a project i am working on, i wrote a WebSite class, which will hold information about a web site. In the database the web sites should be unique, and by business rules uniquness is defined as, no same name web site shall exist twice, no same urls shall exist twice; so in my website class i overloaded == sign to check for these two properties such as :

public static  bool operator == (WebSite site1,WebSite site2)
{
   return site1.Equals(site2);
}

public override bool Equals(object obj)
{
    WebSite temp = (WebSite)obj;
    if(this.Name==temp.Name) return true;
    if(this.Url==temp.Url) return true;
    return false;

}

 

At the first glance, the code seemed right to me; however what is WebSite2 is null, so we should check it right ? i changed the code as below:

and... my code broke. Himm where was the problem ? there is a recursive call in the null check :) so i  have to typecast upto object before  i do the null check, or before typecasting it to website i have to do the null check such as :

 

By the way i catch this error with unit testing :)) 

Tags:

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



About the author

Volkan Uzun




E-mail me Send mail

Twitter

Calendar

<<  December 2008  >>
MoTuWeThFrSaSu
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar

Flickr Badge

www.flickr.com
This is a Flickr badge showing public photos from volkanuzun. Make your own badge here.

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Sign in