Link to home
Start Free TrialLog in
Avatar of martypitt
martypitt

asked on

Correct use of Nullable types

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
Avatar of ericwong27
ericwong27
Flag of Singapore image


   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
Avatar of martypitt
martypitt

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of ericwong27
ericwong27
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial