More In-Depth About Nulls And DBNull
page 2 of 7
by Brian Mains
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 33776/ 54

.NET Types

The .NET Framework comes with many different types defined within it.  A type is a class, structure, or other declaration; for instance, int, short, string, DataTable, List<string> are types, with the last one being a generic type.  The following code creates an instance of a type:

Listing 1

int I = 0;

The variable I is of type int, and is an instance of the int type with the value zero.  There are two kinds of types; value types and reference types.  Reference types are defined as a class.  For instance, the following is a reference type:

Listing 2

public class MyClass 
{ 
}

MyClass is a reference type, which means it's stored on the heap, and that it can be explicitly set to null.  Some of the reference types defined in the framework are: string, DataTable, DataSet, List<T>, and so on.  On the other hand, value types are defined as structures, but the value types int, short, etc. inherit from the base class ValueType.  The following is a value type:

Listing 3

public struct MyValueType 
{ 
}

Structures have certain rules, like they need to have at least one field defined within it, as well as a few other rules.  Some of the common value types are: DateTime, Rectangle, int, short, long, and many more.

There are many articles on the web discussing the differences between value and reference types.  Please consult them for more information.


View Entire Article

User Comments

Title: Nasty syntax for getting round this   
Name: Tyrannosaurs
Date: 2008-02-20 11:50:11 AM
Comment:
You cast a null date time to a dbnull like this:

datetime? myDateTime;

myDateTime = null;

.... parameters.add(...).value = (object)myDateTime ?? System.DBNull.Value;

It's pretty horrible but works and is no worse than most of the alternatives.
Title: DBNULL   
Name: g
Date: 2007-11-19 6:37:19 AM
Comment:
this concept can be dealt in more detail
Title: Datetime & DBNull Reply   
Name: Brian
Date: 2007-11-10 11:29:55 PM
Comment:
You can't cast directly; DBNULL and DateTIme are two separate object types, and you can't cast one to the other. However, in an ADO.NET datarow object, the underlying value is object, which could accept both. However, if the field is not null, then you can't assign a null value to it. It all depends on your code.
Title: Datetime&DBnull   
Name: Erick
Date: 2007-11-09 6:49:21 AM
Comment:
When I assigned DBNull.Value to System.Datetime, it raised a exception. Can I set a Datetime field of the database to null by using DBNull.Value in ADO.net?






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-26 12:43:28 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search