Nullable DateTime variables

C

Child

I have a class object with a couple of datetime variables that are not
required (for example the closing date for an event). I have been doing
some research on usenet history to find that this is confusing to manage and
that in different versions of ASP .Net this is handled differently.

While the database field is a datetime (nullable) the class variable doesn't
need to be.

What would be the best way to handle this variable?
 
B

Brock Allen

Use SqlDataTime in your classes instead of DateTime. They're designed exactly
for this purpose.
 
S

Steve C. Orr [MVP, MCSD]

The Date variable type cannot be null. It must have a value.
Most people use Date.MinValue (or Date.MaxValue) to represent a null date in
their code.

Example:
Dim MyDate as Date = Date.MinValue

As for inserting it in the database, the other guys already specified that
DBNull.Value is what you'll want to use.

The better news is that in .NET 2.0 all variable types will be nullable.
Here's more info:
http://www.panopticoncentral.net/archive/2004/06/04/1180.aspx
 
B

Brock Allen

The better news is that in .NET 2.0 all variable types will be
nullable.

Well, the problems with this are 1) The SqlServer specific ADO.NET APIs don't
support them (like they do for the SqlXxx types) and 2) The logic of the
nullable types is not the same as the logic mandated by a database. For example:

void Main()
{
SqlInt32 x = SqlInt32.Null;
SqlInt32 y = 5;
Console.WriteLine(x == y);

int? x2 = null;
int? y2 = 5;
Console.WriteLine(x2 == y2);
}

This prints out:
Null
False

The later does not jive with what a DBA would say :)
 
B

Brock Allen

And I guess to follow up on this, the obvious question would be "Umm, ok,
then where are these useful?". Well, it turns out they serialize quite well
to XML when using the XmlSerializer, as they'll emit xsi:nil='true' attribute
when Nullable<T>.HasValue == false. Whether or not that's worth a change
in the language, you be the judge :)
 
Joined
Jan 27, 2010
Messages
27
Reaction score
0
Nullable Type in .NET

The nullable type can represent a normal or regular set of values for its value type including the null value.

If we take an example of bool type, then Nullable<bool> can contain set of values like true or false or null.So, assigning null value to bool is useful when we are dealing with database in which the Boolean field may store value like true or false or it may be sometimes undefined.

Nullable Types are instances of System.Nullable struct.

Declaration:
A nullable type can be declared similar to normal variable but with ? modifier at the end of keyword.For example,
int? number = null;
Here ? denotes that the object is a Nullable object of that specific datatype(here it is int).

Alternative way of declaring Nullable type is,
Nullable<int> number = null;

Properties:
An instance of nullable has two public properties:
1.HasValue : This property returns true if the variable contains a value, or false if it is null.
2.Value : This property returns a value if one is assigned, otherwise it will throw an exception.(InvalidOperationException)
So,to avoid this exception, the HasValue member is used to test if the variable contains some value before doing any operation with the nullable types. For example:

if(number.HasValue)
{
int num = number.Value;
}
 

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

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top