Since relational database support null values for every types, it makes sense that your application should also support null values. Reference types can directly take null values but value types are always initialized with their default values that makes it difficult to figure out whether they contain some actual value or not.
One way developers used to achieve this by assigning some hypothetical value to a value type so that at any point of time looking at the value they would come to know whether that value type variable actually has been assigned or not.
With .Net 2.0 it has been possible to create nullable value types that means you can assign a null value to a value type with keeping all other value type advantages intact. Nullable types can be declared in following manner
Dim myNullable As Nullable(Of Integer)
myNullable = 4
Dim mynull As Integer? = 4
if you are interested to know what type it returns for myNullable and myNull so it is "Integer?" in both of the cases. Interestingly Integer? is nothing but a wrapper over Integer class which provides an additional functionality.
Nullable types contain a property called HasValue which provides you a better way to find out whether any value has been assigned by any time to your variable or not
Dim hasSomeValue As Boolean = myNullable.HasValue
will return me true or false based on myNullable has a null value or a valid integer.
nullable types are initialized with 'NULL' in C# and with 'NOTHING' in vb.net
Tuesday, June 22, 2010
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment