NULLs

  • Thread starter Thread starter Mike
  • Start date Start date
M

Mike

Hi.

I have a database from which I am reading some integers into my C# app. The
problem is some of these DB values are NULL, so obviously an error is thrown
when I try to read them. Is there a standard way of representing a NULL
value type? Anyone got any tips on this?

Mike
 
Hi Mike,
Check for DBNull(.Value)

Yeah, I realise I can check for the NULL first, but I want to be able to
represent this NULL value in my app. Like in the database, I can set an
INTEGER field to NULL, but in C#, I can't set an int variable to NULL. How
do people get around this?

Mike
 
Well, for positive numbers, I would use -1. For both negative and positive numbers ... um ... maybe pick a number, any number and treat it as NULL, Int32.MinValue for instance.
 
Mike said:
Yeah, I realise I can check for the NULL first, but I want to be able to
represent this NULL value in my app. Like in the database, I can set an
INTEGER field to NULL, but in C#, I can't set an int variable to NULL. How
do people get around this?

Mike

I think you are asking for nullable types which will be available in C# 2.0.
Support for nullable types was added to have the language interact with
databases.
Hence you will in the near future be able to write:

int? a = 5;
int? b = null;
int? c = a + b;

C would then contain null.

If you want nullable types today I guess you have to play around with a
DataSet.

Best Regards
- Michael S
 
Yeah, I realise I can check for the NULL first, but I want to be able to
I think you are asking for nullable types which will be available in C# 2.0.
Support for nullable types was added to have the language interact with
databases.
Hence you will in the near future be able to write:

int? a = 5;
int? b = null;
int? c = a + b;

C would then contain null.

That's exactly what I was asking, thanks.

Another question on nulls. I'm trying to override the == for a class I have
created:


public class Termination
{
public string CountryName, TerminationName;

public Termination(string countryName, string terminationName)
{
this.CountryName = countryName; this.TerminationName = terminationName;
}

public override string ToString()
{
string str = this.CountryName;
if(this.TerminationName != "" && this.TerminationName != null)
str += " - " + this.TerminationName;
return str;
}

// .................

public static bool operator==(Termination t1, Termination t2)
{
return (t1 == null && t2 == null) ||
(t1 != null && t2 != null && (String.Compare(t1.ToString(),
t2.ToString()) == 0));
}

// .................

}


This is all good if I do this:


Termination t1 = new Termination("USA", "Washington");
Termination t2 = new Termination("UK", "London");
Console.WriteLine(t1 == t2); // False


But this throws "An unhandled exception of type
'System.StackOverflowException'":


Termination t1 = null;
Console.WriteLine(t1 == null); // should be true


presumably because I am defining the == operator WITH the == operator,
causing infinite recursion at runtime. I originally tried this for my
override method:


public static bool operator==(Termination t1, Termination t2)
{
return (String.Compare(t1.ToString(), t2.ToString()) == 0));
}


but that gives "An instance of the object not set" at runtime when comparing
t1 to null.

How do I avoid this?

Mike
 
Hi Mike,


I havent run your code but I think that you are using your overloaded
ToString() method in the == method, then at the same time you are using
ToString() inside the == method, therefore you enter in an infinite cicle
where one method calls the other, hence the stack overflow exception :)


Cheers,
 
Hi Mike,

If you google this you will find a lot of post related to this. Basically
you have two options ( the third being wait until C# 2.0 ships :) )
1- Use an especific integer value to indicate null , this is dependand of
your application .
2- Create/Use a wrapper class.

Cheers,
 
Hi Mike,
I havent run your code but I think that you are using your overloaded
ToString() method in the == method, then at the same time you are using
ToString() inside the == method, therefore you enter in an infinite cicle
where one method calls the other, hence the stack overflow exception :)

Yes, my stack overfloweth.

Mike
 
Michael and Mike,

Don't forget that you can use the SqlInt32 structure as well, which has
null semantics.

Hope this helps.
 
Nicholas Paldino said:
Michael and Mike,

Don't forget that you can use the SqlInt32 structure as well, which has
null semantics.

Hope this helps.

Yes, a bit more lean method than my suggestion of using a whole bloated
DataSet! =)

Thanks
- Michael S
 
Back
Top