Solved

Correct use of Nullable types

Posted on 2006-06-13
3
293 Views
Last Modified: 2012-08-14
Hi

I'm not sure if I'm overcomplicating something here, and using the concept of nullable types in the wrong way.

I have a class whos constructor looks something like:

Public sub New(varA as double, varB as double)

It's valid for either varA or varB to be null, and doing so would result in different behaviour in the underlying construction.

To allow the passing of nulls, I have declared this as:

Public sub New(varA as Nullable(of double), varB as Nullable(of double))

Is this the correct / intended use of Nullable, or am I committing sin here?

Marty
0
Comment
Question by:martypitt
  • 2
3 Comments
 
LVL 10

Expert Comment

by:ericwong27
ID: 16899400

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim obj1 As New SampleClass(Nothing, Nothing)
       
    End Sub

   Public Class SampleClass

        Public Sub New(ByVal varA As Nullable(Of Double), ByVal varB As Double)

            ' -1 means no value is defined.
           MsgBox(varA.GetValueOrDefault(-1))

           ' value type is not nullable, 0 will return is no value is define
           MsgBox(varB)

       End Sub

  End Class
0
 

Author Comment

by:martypitt
ID: 16916549
Hi Eric

That wasn't really my question.

I understand the ways to use Nullabe(of T), my question is around whether this is what it was intended for, or -- in my case -- if there is a more appropriate appraoch.
0
 
LVL 10

Accepted Solution

by:
ericwong27 earned 250 total points
ID: 16925091
When you create an instance of Nullable you specify the data type. If you don't set a value, this instance contains a null reference.

A Nullable<bool> can be assigned the values true or false, or null. The ability to assign null to numeric and Boolean types is particularly useful when dealing with databases and other data types containing elements that may not be assigned a value. For example, a Boolean field in a database can store the values true or false, or it may be undefined.


0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Encryption Decryption in Oracle 12 60
.net VBA word safemode 1 24
Setting runtime form location 4 19
Showdialog 8 20
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now