Solved

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

Posted on 2006-06-13
7
257 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
[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
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 86

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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 86

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

707 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