-
February 18th, 2009, 05:42 PM
#1
testing if an object is null
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?
-
February 18th, 2009, 05:54 PM
#2
Re: testing if an object is null
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 05:56 PM.
-
February 18th, 2009, 05:54 PM
#3
Re: testing if an object is null
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.
www.monotorrent.com For all your .NET bittorrent needs
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.
-
February 18th, 2009, 05:57 PM
#4
Re: testing if an object is null
Originally Posted by Mutant_Fruit
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.
+1 rep if I could.
-
February 18th, 2009, 06:06 PM
#5
Re: testing if an object is null
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.
-
February 18th, 2009, 06:20 PM
#6
Re: testing if an object is null
Originally Posted by afireohno
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.
-
February 18th, 2009, 06:25 PM
#7
Re: testing if an object is null
Sometimes I think they give assignments with ridiculous specifications just to confuse and weed people out of the program.
-
February 19th, 2009, 12:43 AM
#8
Re: testing if an object is null
Originally Posted by afireohno
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
-
February 19th, 2009, 02:07 AM
#9
Re: testing if an object is null
Use static Object.ReferenceEquals() method:
Code:
if (Object.ReferenceEquals(myObject, null))
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|