How can I test to see if an object is null.
Everything I've found says I should use:
Code:
if (myObject == null)
Unfortunately I've overloaded the == operator so this won't work. Is there another way to do this or something I could do to my overloaded operator to handle this situation?
Why wouldn't that work? If your overload cannot handle a comparison to null then you need to fix it. I assume that you overrode .Equals() as well, right?
Last edited by BigEd781; February 18th, 2009 at 04:56 PM.
Then your overloaded operator should do a nullcheck before it does the usual equality check.
Alternatively if you cast to 'object' first, you'll use the regular (non-overloaded) comparison:
Code:
if ((object)myObject == null) {
Console.WriteLine ("I'm using the regular '==' and i'm null");
}
EDIT: Generally speaking you should only override == for immutable types. So if your type is mutable, then don't override '=='. If you want value comparison, you should be overriding the 'Equals' method.
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
EDIT: Generally speaking you should only override == for immutable types. So if your type is mutable, then don't override '=='. If you want value comparison, you should be overriding the 'Equals' method.
Unfortunately this is a school project so the choice to use .Equals() as opposed to == is not my choice to make. I think the cast will work. I hadn't thought of that. Thanks.
Unfortunately this is a school project so the choice to use .Equals() as opposed to == is not my choice to make. I think the cast will work. I hadn't thought of that. Thanks.
You should let your teacher know that they are not really teaching this concept in the best way.
Sometimes I think they give assignments with ridiculous specifications just to confuse and weed people out of the program.
Agreed
Instructors tend to make you do things in awkward ways, perhaps its because they dont fully understand the concepts. Or because thats the only way they know how and they can grade successfully. My instructor for VB.NET (sorry... but ick) makes us write comments on all of out methods and properties like so:
Code:
' ====================================
' === MyMethod(ByVal myNameString As String)
' === myNameString: The users name.
' === Purpose: Does something with a name.
' ====================================
Personally I think its gaudy, so I came to him one day and told him about auto-generated XML Summary tags and he liked it and changed his curriculum a bit. I told him Ive been using C# for about 3 - 4 years now and Ive discovered alot of tricks that will make everyones life a little easier, lol.
Ok, Im done hijacking this thread
R.I.P. 3.5" Floppy Drives
"I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein
Bookmarks