Problems checking for null object

P

pbd22

Hi.

I have a user object and I want to check for nullity.
I want to know if no data has been entered for this
particular object (ie. the user just hit submit w/o
registering... assuming no client validation).

I use:

if (user == null)

which I think is correct. but, i get false. I think the
reason is that the default value for an Int is 0 and
therefor the object is not completely null?

user.first = null
user.last = null
user.day = 0
user.moth = 0
user.year = 0
user.city = null
etc...

thoughts?

thanks.
 
T

Tom Overton

Hi.

I have a user  object and I want to check for nullity.
I want to know if no data has been entered for this
particular object (ie. the user just hit submit w/o
registering... assuming no client validation).

I use:

if (user == null)

which I think is correct. but, i get false. I think the
reason is that the default value for an Int is 0 and
therefor the object is not completely null?

user.first = null
user.last = null
user.day = 0
user.moth = 0
user.year = 0
user.city = null
etc...

thoughts?

thanks.

What type of object is "user"?

If it's a regular instance class then your code should work, assuming
if in your code if the user presses submit without doing data entry
then an instance of the class is never created. If no instance of
your object has been created it will have a null reference (nothing on
the heap yet). This won't work with a static class because by the
time you access the object in code to check it, it won't have a null
value anymore.

So if it's a struct or static class you'll just have to inspect each
field to see if values have been assigned.
 
P

proxyuser

pbd22 said:
Hi.

I have a user object and I want to check for nullity.
I want to know if no data has been entered for this
particular object (ie. the user just hit submit w/o
registering... assuming no client validation).

I use:

if (user == null)

which I think is correct. but, i get false. I think the
reason is that the default value for an Int is 0 and
therefor the object is not completely null?

user.first = null

Of course - if you can access "first", then there must be something there to
access it with. In other words, user is definitely not null in that case
(unless you get a runtime exception.) user itself has to actually be null,
in which case you cannot write things such as "user.first".
 
P

proxyuser

pbd22 said:
Hi.

I have a user object and I want to check for nullity.
I want to know if no data has been entered for this
particular object (ie. the user just hit submit w/o
registering... assuming no client validation).

Once you figure out your null issues, than one obvious solution is to simply
add a flag to the class that is true whenever data is updated in an object
after the constructor runs. Or better yet, the flag only gets set when a
public accessor sets the data. Something to the effect of:

class A

{

int i;

int j;

bool userUpdated = false;

public int I

{

get

{

return i;

}

set

{

i = value;

userUpdated = true;

}

}

public int J

{

get

{

return j;

}

set

{

j = value;

userUpdated = true;

}

}

private void SomeInternalMethod( )

{

i = i + 1;

j = j + 1;

// no user updates here

}

}
 
D

Dawid Rutyna

(...)
I use:

if (user == null)

which I think is correct. but, i get false.
(...)

Solutions for you:
(1) override Equals and operator ==, != ..., this is bad idea
(2) write a method like IsEmpty(), better

class Program
{
static void Main(string[] args)
{
User u;

u = new User();
Console.WriteLine(u == null);
u = new User(null, null, 0);
Console.WriteLine(u == null);
u = new User(string.Empty, string.Empty, 0);
Console.WriteLine(u == null);
u = new User("Dawid", "Rutyna", 24);
Console.WriteLine(u == null);


Console.WriteLine(u.IsEmpty());
}
}

class User
{
private string _name;
private string _surname;
private int _age;

public User()
{
_name = string.Empty;
_surname = string.Empty;
_age = 0;
}

public User(string name, string surname, int age)
{
_name = name;
_surname = surname;
_age = age;
}

public bool IsEmpty()
{
return (string.IsNullOrEmpty(this._name) &&
string.IsNullOrEmpty(this._surname) && _age == 0);
}

public override bool Equals(object obj)
{
if (obj == null)
return (string.IsNullOrEmpty(this._name) &&
string.IsNullOrEmpty(this._surname) && _age == 0);
if (this.GetType() != obj.GetType()) return false;

User u = (User)obj;
if (!Object.Equals(_name, u._name)) return false;
if (!Object.Equals(_surname, u._surname)) return false;
if (!_age.Equals(u._age)) return false;

return true;
}

public static bool operator ==(User u1, User u2)
{
return u1.Equals(u2);
}

public static bool operator !=(User u1, User u2)
{
return !u1.Equals(u2);
}
}

Dawid Rutyna
 

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