?
Solved

Correct use of Nullable types

Posted on 2006-06-13
3
Medium Priority
?
299 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 750 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

800 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