Solved

Correct use of Nullable types

Posted on 2006-06-13
3
298 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

696 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