Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

overloading operator==

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
volkan - 16 Mar 2008 19:58 GMT
Hi,
I have a student class. In this class i overloaded ==,!= and  equals so that
they will compare the studentids ( which is a member of the class ).
However i have a problem with null check.
public static bool operator !=(Student s1, Student s2)
       {
         ...
how can i check if s1 or s2 is null or not in this overload.
if i do s1==null it goes recursive
if i typecast s1 down to object class, it seems not working
any ideas ?
Jon Skeet [C# MVP] - 16 Mar 2008 20:06 GMT
> I have a student class. In this class i overloaded ==,!= and  equals so that
> they will compare the studentids ( which is a member of the class ).
[quoted text clipped - 6 lines]
> if i typecast s1 down to object class, it seems not working
> any ideas ?

Casting s1 up to object should be fine. What problems are you having?
An alternative is to use object.ReferenceEquals().

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

BlackWasp - 16 Mar 2008 21:01 GMT
Have a look here.

http://www.blackwasp.co.uk/CSharpRelationalOverload.aspx

Signature

BlackWasp
www.blackwasp.co.uk

> Hi,
> I have a student class. In this class i overloaded ==,!= and  equals so
[quoted text clipped - 7 lines]
> if i typecast s1 down to object class, it seems not working
> any ideas ?
BlackWasp - 16 Mar 2008 21:06 GMT
D'oh, copy and paste failure!

http://msdn2.microsoft.com/en-us/library/ms173147(VS.80).aspx

Signature

BlackWasp
www.blackwasp.co.uk

> Have a look here.
>
[quoted text clipped - 11 lines]
>> if i typecast s1 down to object class, it seems not working
>> any ideas ?
volkan - 16 Mar 2008 21:58 GMT
thanx solved
> Hi,
> I have a student class. In this class i overloaded ==,!= and  equals so
[quoted text clipped - 7 lines]
> if i typecast s1 down to object class, it seems not working
> any ideas ?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.