Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse nullable parameters in Visual Basic .NET
DescriptionThis example shows how to use nullable parameters in Visual Basic .NET.
Keywordssyntax, methods, overloaded methods, overloading, overload, null parameters, optional, optional parameters, nullable, nullable parameters, missing parameters, Visual Basic .NET, VB.NET
CategoriesSoftware Engineering, Syntax
 

You can make a nullable type to allow a data type that normally cannot accept the value Nothing accept that value. For example, value types such as structures and integers cannot have the value Nothing. When you make such a type nullable, you allow it to take the value Nothing.

To make a data type nullable, add a question mark either after the variable's name or the data type's name as in the following declarations.

    Dim num_points As Integer?
    Dim num_customers? As Integer

To see if a nullable variable contains a value, use its HasValue property. If the variable has a value, use its Value property to get that value.

The following DrawStar method draws a star. The num_points, star_pen, and star_brush parameters can all be Nothing. The star_pen and star_brush parameters can be Nothing because they are object references and those can always be Nothing. The num_points parameter is a nullable integer.

 
' Draw a star.
Private Sub DrawStar(ByVal gr As Graphics, ByVal num_points _
    As Integer?, ByVal bounds As RectangleF, ByVal star_pen _
    As Pen, ByVal star_brush As Brush)
    ' See if num_points is nothing.
    Dim number_of_points As Integer
    If (num_points.HasValue) Then
        number_of_points = num_points.Value
    Else
        number_of_points = 5
    End If

    ' Make room for the points.
    Dim points(0 To 2 * number_of_points - 1) As PointF

    Dim rx1 As Double = bounds.Width / 2
    Dim ry1 As Double = bounds.Height / 2
    Dim rx2 As Double = rx1 * 0.5
    Dim ry2 As Double = ry1 * 0.5
    Dim cx As Double = bounds.X + rx1
    Dim cy As Double = bounds.Y + ry1

    ' Start at the top.
    Dim theta As Double = -Math.PI / 2
    Dim dtheta As Double = Math.PI / number_of_points
    For i As Integer = 0 To 2 * number_of_points - 1 Step 2
        points(i) = New PointF( _
            CDbl(cx + rx1 * Math.Cos(theta)), _
            CDbl(cy + ry1 * Math.Sin(theta)))
        theta += dtheta

        points(i + 1) = New PointF( _
            CDbl(cx + rx2 * Math.Cos(theta)), _
            CDbl(cy + ry2 * Math.Sin(theta)))
        theta += dtheta
    Next i

    ' Fill the star.
    If (star_brush IsNot Nothing) Then _
        gr.FillPolygon(star_brush, points)

    ' Draw the star.
    If (star_pen Is Nothing) Then star_pen = Pens.Black
    gr.DrawPolygon(star_pen, points)
End Sub
 
The method checks whether num_points has a value and sets the value number_of_points to either that value if it exists or the default value of 5 otherwise. The code then creates an array of points representing the star's corners.

Next the code checks the star_brush parameter and fills the star if the brush isn't null.

It then checks the star_pen parameter. If star_pen is null, the program sets it to the stock black pen. Finally the code draws the star using the pen.

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated