473,395 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Properly checking for nulls

What is a better way for checking nulls than using the "==" operator?
For example, .Equals() is better than == for most operations since it
knows the type being checked. However, it doesn't seem to work for
nulls. For example:
object myobject = new object();
myobject = null;
if (myobject.Equals(null))
Debug.WriteLine("no go");

For some reason, I get stuck on the if statement when debugging.
Stepping over just keeps me on the if line. Any suggestions?

Thanks,
Brett

Mar 27 '06 #1
3 1229
== null is the way you are supposed to check for nulls..

..Equals is failing you because its trying to use null to determine
type, but getting the type of a null value results in an exception.
However, Equals swallows all exceptions.

BTW, using .Equals isn't better, and there's no reason to use it over
the == operator. I found its rare to compare two values of differing
types anyway.

Mar 27 '06 #2
..Equals() is better in the case of non nulls. == has no idea what you
are comparing. It could be value or reference types and then system or
custom types. .Equals() doesn't have this issue. You'll notice the
parameter accepts the type you are calling the method on.

Brett

Mar 28 '06 #3
== knows exactly what you are comparing, because it is an operator
defined in a type. == will give you a compiler error if you try to
compare a Guid with a double, for example. Give it a shot.

Also, unless a reference type overrides the default implementation of
Equals, Equals will only return true if the object references are the
same (they are the EXACT same object).

To answer your orginal question, no, there is no other way to test for
null except using ==.

I don't know why you'd want to ignore type when comparing values
anyway. What exactly does it mean for one Stream object to equal a
Guid? It doesn't make sense, which is why == is a defined operator
between the two.

Mar 28 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: JackO | last post by:
I have a text box on a form “txtExamDate” that I know contains nothing. I am trying to test code that will determine the text box is nothing. I am getting the error because there is no value...
0
by: Rhino | last post by:
I am working with SQL Functions in DB2 for Windows/Linux/UNIX (V8.2.1) and am having a problem setting input parameters for SQL Functions to null in the Development Center. My simple function,...
1
by: PST | last post by:
Here's a problem I'm trying to deal with: I'm working on a Frontpage 2000 website for a boat handicapping system, built in Access 97. What I'm trying to accomplish is: The user enters a...
4
by: Rich | last post by:
I have a access form that is connected to linked sql table via odbc. I have some fields that I dont want to allow nulls when data is entered via the form. I can set the null checking on the SQL...
10
by: Regnab | last post by:
I'm trying to test a recordset to see if the SQL query is returning a Null value. However, the IsNull() test always returns a 'False' response. I even tried: Dim NullCheck As String NullCheck =...
2
by: Chris | last post by:
Hi, I have an SQL Query that loads a SQLDataReader object. The returned record has bit datatype columns that I use to provide true/false values. I can't get a definite answer googling on...
5
by: Andy G | last post by:
I'm getting this error...Operator is not valid for type 'DBNull' and string "". What is happening is that I'm calling a stored procedure to use the email address to recover a forgotten username. ...
21
by: onkar | last post by:
#include<stdio.h> int i; int i; int main(){ printf("i=%d\n",i); return 0; } Note : I am using gcc-3.4.3 on i386-redhat-linux
0
by: ricardovillarroel | last post by:
i am trying to check the text box for nulls once the user tabs over the text box. i do not want it to check for nulls when i click on okay. here is code. do i need to setfocus so it will go back to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.