PC Review


Reply
Thread Tools Rate Thread

Comparison to null

 
 
juanph31@hotmail.com
Guest
Posts: n/a
 
      14th Mar 2005
Hello there,

Why is it that I sometimes see this construct

//case 1
if ( null == myVariable)
{
//...etc
}


//case 2
if ( myVariable == null)
{
//...etc
}

Basically, what is the difference between the two?

 
Reply With Quote
 
 
 
 
Scott Klueppel via DotNetMonster.com
Guest
Posts: n/a
 
      14th Mar 2005
To my knowledge, there is no difference.

I was taught to use the if(null == myVariable) method because a mistake
like using a = instead of == will be caught at compile time instead of
runtime.

--
Message posted via http://www.dotnetmonster.com
 
Reply With Quote
 
Christopher Kimbell
Guest
Posts: n/a
 
      14th Mar 2005
It makes no difference, they are the same. The syntax is common for people
that have been programing in C/C++ for many years.

In C/C++ it is legal to write (single '=');

if(myVariable = null)
{
}

If you switch them around, it causes a compilation error because you cannot
assign a value to null.

if(null = myVariable)
{
}

Chris

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello there,
>
> Why is it that I sometimes see this construct
>
> //case 1
> if ( null == myVariable)
> {
> //...etc
> }
>
>
> //case 2
> if ( myVariable == null)
> {
> //...etc
> }
>
> Basically, what is the difference between the two?
>



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      14th Mar 2005
Scott Klueppel via DotNetMonster.com <(E-Mail Removed)> wrote:
> To my knowledge, there is no difference.
>
> I was taught to use the if(null == myVariable) method because a mistake
> like using a = instead of == will be caught at compile time instead of
> runtime.


Both of them will be caught at compile-time with C# though. There's no
reason to use this unnatural ordering in C#.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
juanph31
Guest
Posts: n/a
 
      14th Mar 2005
I swear I saw a code doing like that ( case 1 and case 2) in one method
and throwing difference exception for each. That's why I wanted to
know.

Juanph31

 
Reply With Quote
 
juanph31
Guest
Posts: n/a
 
      14th Mar 2005
I swear I saw a code doing like that ( case 1 and case 2) in one method
and throwing difference exception for each. That's why I wanted to
know.

Juanph31

 
Reply With Quote
 
Bob Calvanese
Guest
Posts: n/a
 
      14th Mar 2005
I don't think that

if(null == MyValue)
{
....
}

is a good habit to get into. I personally have not seen code like that in
any real world projects, but feel it would not be a proper coding standard
even if it makes no difference with comparison operators. And that would go
for any language IMHO.

Bob Calvanese
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello there,
>
> Why is it that I sometimes see this construct
>
> //case 1
> if ( null == myVariable)
> {
> //...etc
> }
>
>
> //case 2
> if ( myVariable == null)
> {
> //...etc
> }
>
> Basically, what is the difference between the two?
>



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      15th Mar 2005
juanph31 <(E-Mail Removed)> wrote:
> I swear I saw a code doing like that ( case 1 and case 2) in one method
> and throwing difference exception for each. That's why I wanted to
> know.


Well, if you could show that code, I'd be interested to see it.
Admittedly there could be a difference if someone had overloaded the ==
operator and done it badly, but that's a bug in their operator
overloading rather than a feature of the language.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
=?UTF-8?B?TWFyY2luIEdyesSZYnNraQ==?=
Guest
Posts: n/a
 
      15th Mar 2005
Hi All,

I think that a "key" is operator overloading.
Like a Jon post it before.

MyClass myVar;

if( null==myVar ) {
}
Doesn't have a chance to fire MyClass "==" operator function.

Marcin

> juanph31 <(E-Mail Removed)> wrote:
>
>>I swear I saw a code doing like that ( case 1 and case 2) in one method
>>and throwing difference exception for each. That's why I wanted to
>>know.

>
>
> Well, if you could show that code, I'd be interested to see it.
> Admittedly there could be a difference if someone had overloaded the ==
> operator and done it badly, but that's a bug in their operator
> overloading rather than a feature of the language.
>

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      15th Mar 2005
Marcin Grz?bski <(E-Mail Removed)> wrote:
> I think that a "key" is operator overloading.
> Like a Jon post it before.
>
> MyClass myVar;
>
> if( null==myVar ) {
> }
> Doesn't have a chance to fire MyClass "==" operator function.


Yes it does. Compile and run the following, ignoring the warnings:

using System;
using System.Data;

class Test
{
static void Main()
{
Test t = new Test();
Console.WriteLine (t==null);
Console.WriteLine (null==t);
}

public static bool operator == (Test t1, Test t2)
{
Console.WriteLine ("Operator== called");
Console.WriteLine ("t1={0}", t1);
Console.WriteLine ("t2={0}", t2);
return true;
}

public static bool operator!= (Test t1, Test t2)
{
return false;
}
}

The output is:
Operator== called
t1=Test
t2=
True
Operator== called
t1=
t2=Test
True

In other words, the overloaded operator is called both times.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Fill null fields from last non-null record - No update query pleas mattsmom Microsoft Access 2 2nd Apr 2008 01:56 PM
Complex comparison of Columns of Data: Extracting unique records after comparison on 4 levels ap Microsoft Excel Programming 2 23rd Jan 2007 10:12 AM
Empty text values aren't null or zero-length, but 2-byte null (\x0000) Mark Steward Microsoft Access 2 21st Jan 2006 02:03 PM
MD5 generation/comparison and Strings comparison Mike Ji Freeware 0 15th Nov 2004 11:57 AM
Null result when combining null field with non-null field in ADP View Lauren Quantrell Microsoft Access Form Coding 8 17th Nov 2003 02:34 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:18 AM.