Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can't set variable to null in some type.

0 views
Skip to first unread message

Jongmin

unread,
Aug 4, 2004, 4:09:18 PM8/4/04
to
Hi,

I have made two classes, which are simple and almost same,
T_A and T_B.
T_A has a static method overloaded to "==".
T_B dosn't.

And, I run the following code. I expect both "a" and "b"
show the messagebox. But, only "b" showes the messagebox.
Even I try to see "a" through visual studio, "a" is
<undefined value>, null. and "a == null" is "true".

Whats' wrong with it?

Thanks,
Jongmin
// ------> Code snippet
T_A a = null;
if (a == null)
MessageBox.Show("T_A is null"); // it is not shown.

T_B b = null;
if (b == null)
MessageBox.Show("T_B is null");


// ----> full Code

using System;
using System.Collections;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;

public class MyClass
{
public static void Main()
{
Test();
}

public static void Test()
{
T_A a = null;
if (a == null)
MessageBox.Show("T_A is null");

T_B b = null;
if (b == null)
MessageBox.Show("T_B is null");
}
}


public class T_A
{
private int _Idx = 0;
public int Idx
{
get {return _Idx;}
set {_Idx = value;}
}

public override string ToString()
{
return Idx.ToString();
}


public override int GetHashCode()
{
return this.Idx;
}

public override bool Equals(object obj)
{
if (obj==null) return false;
if (this.GetType() != obj.GetType())
return false;

T_A other = (T_A)obj;
return (this.Idx == other.Idx);
}

public static bool operator ==(T_A a, T_A b)
{
if ((object)a == null) return false;
return a.Equals(b);
}

public static bool operator !=(T_A a, T_A b)
{
return !(a==b);
}
}

public class T_B
{
private int _Idx = 0;
public int Idx
{
get {return _Idx;}
set {_Idx = value;}
}
}

Ryan Riddell

unread,
Aug 4, 2004, 4:21:03 PM8/4/04
to
The problem is in the overloaded comparison operator. The first line is:

if ((object)a == null) return false;

Since 'a' is null it is returning false. If you remove that line, it will still return false. The reason is will still be false is because in the Equals function you do:
if (obj==null) return false;

Again, since you are compairing to null it is passing null. Since null == null it will return false.

HTH


"Jongmin" wrote:

> Hi,
>
> I have made two classes, which are simple and almost same,
> T_A and T_B.
> T_A has a static method overloaded to "==".
> T_B dosn't.
>

Jongmin

unread,
Aug 4, 2004, 4:26:22 PM8/4/04
to

I appand the two line to return true on null==null.
Is there any possibility to make problem or side effect?

thanks,
Jongmin

public override bool Equals(object obj)
{

// I appand this code to compare null == null
if ((object)a == null && (object)b == null)
return true;

if (obj==null) return false;
if (this.GetType() != obj.GetType())
return false;

T_A other = (T_A)obj;
return (this.Idx == other.Idx);
}

>.
>

Ryan Riddell

unread,
Aug 4, 2004, 4:37:01 PM8/4/04
to
Response In-Line

"Jongmin" wrote:

>
> I appand the two line to return true on null==null.
> Is there any possibility to make problem or side effect?
>
> thanks,
> Jongmin
>
> public override bool Equals(object obj)
> {
> // I appand this code to compare null == null
> if ((object)a == null && (object)b == null)
> return true;

At this point you don't have access to 'a' and 'b'. Both of these objects are out of scope. You need:


public static bool operator ==(T_A a, T_A b)
{

if ((object)a == null && (object)b == null) return true;

return a.Equals(b);

Jon Skeet [C# MVP]

unread,
Aug 5, 2004, 3:14:09 AM8/5/04
to
Ryan Riddell <RyanR...@discussions.microsoft.com> wrote:
> > I appand the two line to return true on null==null.
> > Is there any possibility to make problem or side effect?
> >
> > thanks,
> > Jongmin
> >
> > public override bool Equals(object obj)
> > {
> > // I appand this code to compare null == null
> > if ((object)a == null && (object)b == null)
> > return true;
>
> At this point you don't have access to 'a' and 'b'. Both of these
> objects are out of scope. You need:
> public static bool operator ==(T_A a, T_A b)
> {
> if ((object)a == null && (object)b == null) return true;
> return a.Equals(b);
> }

That throws an exception if a is null but b isn't. I prefer:

if (object.ReferenceEquals(a, b))
{
return true;
}
if (object.ReferenceEquals(a, null) ||
object.ReferenceEquals(b, null))
{
return false;
}
return a.Equals(b);

--
Jon Skeet - <sk...@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

0 new messages