• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 272
  • Last Modified:

can i check if a property of type integer has been assigned a value

Hi

Is it possible to tell if a property of type integer has been assigned a value or if the underlying variable is still uninitialised.

I have a vague recollection of something called nullable types and I dont know if they are relevant.

Thanks
amdrea
0
andieje
Asked:
andieje
3 Solutions
 
sk33v3Commented:
Dim IntTmp As Integer
        If IntTmp = Nothing Then
            MsgBox("IntTmp is still null")
        Else
            MsgBox("IntTmp equals " & IntTmp)
        End If
        IntTmp = 1
        If IntTmp = Nothing Then
            MsgBox("IntTmp is still null")
        Else
            MsgBox("IntTmp equals " & IntTmp)
        End If

you will notice the first time through that it returns that inttmp is null.
0
 
ZeonFlashCommented:
Are you talking about a variable, or an actual property?  If you have an integer property that is something like the following, then X is automatically initialized to 0.

    Public Property X() As Integer
        Get
            Return X
        End Get
        Set(ByVal Value As Integer)
            X = Value
        End Set
    End Property
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
VB.Net 2005 (.Net 2.0) has the concept of Nullable types:

        Dim IntTmp As Nullable(Of Integer)

        If Not IntTmp.HasValue Then
            MsgBox("IntTmp has no value")
        Else
            MsgBox("IntTmp = " & IntTmp.Value)
        End If

        IntTmp = 1
        If Not IntTmp.HasValue Then
            MsgBox("IntTmp has no value")
        Else
            MsgBox("IntTmp = " & IntTmp.Value)
        End If
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
andiejeAuthor Commented:
Hi

ZeonFlash says that if i have a property as he describes it will automatically be initialized to 0.

Is there any way I can set up an integer property in vb.net 1.1 so I can tell if the property has not been assigned a value
0
 
sk33v3Commented:
Public Property X() As Integer
        Get
            If IntTmp = Nothing Then
                MsgBox("My custome Error message would appear hear or do whatever you need.")
            Else
                return X
            End If
        End Get
        Set(ByVal Value As Integer)
            X = Value
        End Set
    End Property
0
 
sk33v3Commented:
Public Property X() As Integer
        Get
            If IntTmp = Nothing Then
                MsgBox("My custom Error message would appear hear or do whatever you need.")
            Else
                return IntTmp
            End If
        End Get
        Set(ByVal Value As Integer)
            IntTmp  = Value
        End Set
    End Property

Sorry about that. Buggered it up. This is a combination of my code and Zeon's and should do the trick.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You could encapsulate that in a class...

    Public Class MyInteger

        Private _Integer As Integer

        Public Sub New()
        End Sub

        Public Sub New(ByVal value As Integer)
            Me.Value = Value
        End Sub

        Public Property Value() As Integer
            Get
                Return _Integer
            End Get
            Set(ByVal Value As Integer)
                _Integer = Value
            End Set
        End Property

        Public ReadOnly Property HasValue() As Boolean
            Get
                Return Not (Me.Value = Nothing)
            End Get
        End Property

    End Class

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim intA As New MyInteger
        Dim intB As New MyInteger(5)
        Debug.Print("intA initialized:" & intA.HasValue)
        Debug.Print("intB initialized:" & intB.HasValue)

        Debug.Print("intA = " & intA.Value)
        If Not intA.HasValue Then
            intA.Value = 10
        End If
        Debug.Print("intA = " & intA.Value)
        Debug.Print("intB = " & intB.Value)
    End Sub


0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now