NULL values

  • Thread starter Thread starter James Fisher
  • Start date Start date
J

James Fisher

How are people handling NULL values for value types.

Descriptions of problem

Say you have a typical method with the signature:

public int InsertPerson(string firstName, string lastName, int
personCategoryID)

Now say you only want the firstName and lastName parameters to be
required and to retain the NULL value in the database for
personCategoryID. Here are a couple typical solutions for this
scenerio would be:

A. Overload the InsertPerson method and create the following method.

public int InsertPerson(string firstName, string lastName)

So when you detect in your UI that personCategoryID is to be NULL or
"empty" you will call InsertPerson with two parameters. Therefore,
the personCategoryID field in the database will remain NULL.

B. Treat the int as an object and use boxing and unboxing. In my
example convert the int parameter to an object type and check for null
in the method so I can set the stored procedure parameter to DBNull.



My slant on the problem

I happen to have inserts for different types of "People" that have
many (30-40) parameters in their insert methods. So option A above
would not be feasable and option B is more likely however I would have
to modify alot of code to change all my int it objects and use boxing
and unboxing(but certainly feasible). I am interested to hear how
other people are handling this problem.

JF
 
James said:
public int InsertPerson(string firstName, string lastName)

Instead of overloading, you could change the signature to accept an
object.

Personally, I prefer to manipulate the data directly within a DataSet.
 
Hi all
From my understanding of the framework etc. everything is inherited from an
object even system types such as int.

You can declare a variable as an int with a value of null
eg int personCategoryID = null;

The framework will then give it the default value of null for an int which
is 0.

You can then test for this in your function - e.g.
public int InsertPerson(string firstName, string lastName, int
personCategoryID)
{
if (personCategoryID != null)
{
//code
}
}

The only downside to this is if you use the default value (i.e. 0 in this
case) in your database, but thats just a management thing - just start from
1.

Of course the other aregument about whether you should use integers or GUIDs
for IDs comes to mind to. I would use the GUID and test for null (again a
string of 0's in the GUID format)

Hope it helps
Mark
 
This won't work; you can't set a value type to null.

Yes bjs is correct,

There really is no great solution to this. If I were using VB.NET I
could use the combination of Nothing and IsNull and not have to change
much. However, using C# I created a wrapper type for my value types.
For example, I created an Integer class that wraps the good ole' int.
It has two properties which are IsNull and Value. So by changing my
object properties to Integers lets me check whether or not they are
null before using them and properly inserting a NULL into the
database.

JF
 
Back
Top