www.itgalary.com Homepage
Forum Home Forum Home > Programming > VB & VB.net
  Active Topics Active Topics
  FAQ FAQ  Forum Search   Register Register  Login Login

Nullable Types in VB 2008 & VB 2005

 Post Reply Post Reply
Author
Message
  Topic Search Topic Search  Topic Options Topic Options
Jijo View Drop Down
Admin Group
Admin Group
Avatar

Joined: 31 Dec 05
Location: United Kingdom
Posts: 495
  Quote Jijo Quote  Post ReplyReply Direct Link To This Post Topic: Nullable Types in VB 2008 & VB 2005
    Posted: 03 Sep 07 at 8:30am
Nullable Types

Relational databases present semantics for nullable values that are often inconsistent with ordinary programming languages and often unfamiliar to programmers. In data-intensive applications, it is critical for programs to handle these semantics clearly and correctly. Recognizing this necessity, with .NET Frameworks 2.0 the CLR has added run-time support for nullability using the generic type Nullable(Of T As Structure). Using this type we can declare nullable versions of value types such as Integer, Boolean, Date, and so on. For reasons that will become apparent, the Visual Basic syntax for nullable types is T?.

For Example we have a member class. With Name,DateOfBirth and WeddingDate. But will all be married so what should be the WeddingDate for such persons, its should be null rather than setting to something else.

Partial Class Member
public Name as String
Public WeddingDate As Date?
End Class

Dim MemberA =   New Country With { .Name = "ITGalaryMemberA", .WeddingDate = #10/1/1994# }
Dim MemberB =   New Country With { .Name = "ITGalaryMemberB", .WeddingDate = Nothing }

Don't get confused the above initialization of class with 'With' is new to Orcas/VB 9.0/VB 2008

So this Nullable Types will solve the problem. We can check

This was there in VB 2005 as well. Lets make a function which takes integer and show the square. If value is Nothing it should show cannot find the square. So we need to declare Integer which can be Nothing. The code will look like something below.

Dim myIntVariable As Nullable(Of Integer)
myIntVariable = Nothing
'myIntVariable = 5   'Uncomment this to see sqaure.
Call Test(k)


Sub Test(ByVal myInt As Nullable(Of Integer))
    If myInt.HasValue() Then
        MsgBox(myInt.Value * myInt.Value)
    Else
        MsgBox("cannot find the square")
    End If
End Sub

If you were not using Nullable you can see that the variable can be set to Nothing but when passed to the method it get initialised as zero.
 
What is new in VB 2008
 
What are Extension Method
 
Partial Methods in C# 3.0 and VB.net 9.0
 
IIf in VB2005 vs IF in VB2008 - Whats new in VB9.0
 
Relaxed Delegates. VB 2008 / VB 9.0
 


Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

.