Can't set variable to null in some type.

J

Jongmin

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;}
}
}
 
J

Jongmin

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);
}

-----Original Message-----
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.
 
J

Jon Skeet [C# MVP]

Ryan Riddell said:
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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top