473,377 Members | 1,151 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,377 software developers and data experts.

NULL values

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
Nov 15 '05 #1
4 16171
James Fisher wrote:
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.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
Nov 15 '05 #2
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
"James Fisher" <jf*****@snip.net> wrote in message
news:76**************************@posting.google.c om...
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

Nov 15 '05 #3
su******@bellsouth.net (bjs10) wrote in message news:<58**************************@posting.google. com>...
This won't work; you can't set a value type to null.

"Mark" <f1*****@hotmail.com> wrote in message news:<#d*************@TK2MSFTNGP10.phx.gbl>...
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.


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
Nov 15 '05 #4
su******@bellsouth.net (bjs10) wrote in message news:<58**************************@posting.google. com>...
This won't work; you can't set a value type to null.

"Mark" <f1*****@hotmail.com> wrote in message news:<#d*************@TK2MSFTNGP10.phx.gbl>...
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.


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
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Marcus | last post by:
Hello, quick question about MySQL storing NULL values... Say I have a textbox called $_POST and a variable $var. if(empty($_POST)) $var = NULL; else $var = $_POST; Disregarding...
26
by: Agoston Bejo | last post by:
I want to enforce such a constraint on a column that would ensure that the values be all unique, but this wouldn't apply to NULL values. (I.e. there may be more than one NULL value in the column.)...
4
by: Ellen Manning | last post by:
Using SQL2000. I want to return the # of columns with non-null values. Here's my query so far: select case when Dx1 is not null then 0 else 1 end + case when Dx2 is not null then 0 else 1 end...
3
by: iStrain | last post by:
Hiya. I'm _sure_ this is an FAQ, but Googling hasn't produced the answer in a way I can make sense out of. I know I should get this, but so far no way... I'm creating tables and doing queries in...
8
by: manning_news | last post by:
Using SQL2000. According to Books Online, the avg aggregrate function ignores null values. ((3+3+3+3+Null)/5) predictably returns Null. Is there a function to ignore the Null entry, adjust the...
13
by: Federico Balbi | last post by:
Hi, I was wondering if PGSQL has a function similar to binary_checksum() of MS SQL Server 2000. It is pretty handy when it comes to compare rows of data instead of having to write long boolean...
17
by: Mark A | last post by:
DB2 8.2 for Linux, FP 10 (also performs the same on DB2 8.2 for Windoes, FP 11). Using the SAMPLE database, tables EMP and EMLOYEE. In the followng stored procedure, 2 NULL columns (COMM) are...
15
by: info | last post by:
Hi, I need this behaviour: 1 + null = 1 I have a (dynamic) set of many columns containing decimals that I want to add as follows: if all columns are null the result should be null if not all...
3
ADezii
by: ADezii | last post by:
Null as it relates to database development is one of life's little mysteries and a topic of total confusion for novices who venture out into the database world. A Null Value is not zero (0), a zero...
10
by: Toby Gallier | last post by:
Hello! I have a form that is calculating averages as follows: " =(NZ()+Nz()+Nz())/3 " However I need to now adjust for null values , so for example if value2 is null I would then need to...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.