Solved

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

Posted on 2006-06-13
7
252 Views
Last Modified: 2010-04-23
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
Comment
Question by:andieje
7 Comments
 
LVL 9

Assisted Solution

by:sk33v3
sk33v3 earned 350 total points
ID: 16897064
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
 
LVL 17

Expert Comment

by:ZeonFlash
ID: 16897091
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 16897242
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:andieje
ID: 16897597
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
 
LVL 9

Expert Comment

by:sk33v3
ID: 16897652
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
 
LVL 9

Accepted Solution

by:
sk33v3 earned 350 total points
ID: 16897658
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
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 150 total points
ID: 16897845
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

789 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