Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2006-06-13
7
Medium Priority
?
262 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 1400 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 1400 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 600 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

618 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