Original Post
Ok, so I have an object called Date. For any two dates A and B the following conditions must be met in order for them to be equivalent A.day == B.day A.month == B.month A.year == B.year For simplicities sake I'd like to be able to simply write: A == B So I added this function to my Date class. Sweet, so everything is dandy. Except Date should still be just an object (that is allocated on the heap with a pointer), so I expected the following to be a valid test: Instead of getting a true or false result, an exception is thrown claiming I'm trying to access a null reference. Upon further inspection, the error was thrown from the overloaded == operator in that B was assigned "null", so when it tried to evaluate B.day I got nailed. I guess that kinda makes sense (altho, not really to me). So I tried modifying the overloaded operator using the following code (although I was sure it wouldn't work). And as expected I just end up with a stack overflow (due to the infinite recursion). If anyone has any pointers for me, that'd be great. Am I overloading == wrong? Or is there a different way to check if the pointer is null within the == operator? I'm getting a warning that I should overload Object.Equals in Date. Is there some way to use this to check if the pointer is a null object? Thanks for your time and consideration, ~FenixRoA
[source lang = "c#"]
public static operator ==(Date A, Date B)
{
return A.day == B.day && A.month == B.month && A.year == B.year;
}
[source lang = "c#"]
Date outp;
...
if (outp == null)
...
[source lang = "c#"]
public static operator ==(Date A, Date B)
{
if (A == null || B == null)
return A == B;
return A.day == B.day && A.month == B.month && A.year == B.year;
}