Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Correct use of Nullable types

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

604 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