Solved

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

Posted on 2006-06-13
7
246 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now