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

Default values for a property in a class VB 2005

VB 2005 Express: Is there a way to define default values for a property? Simple example, when you type = after a boolean property, say... visible, it pops up intellisense true and false, is there a way to make custom options in place of true or false?
0
ckanoab
Asked:
ckanoab
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Define an Enumeration.

After the equals you would get the list of wet, dry and dirty:

        Dim d As New Dog
        d.MyDogsNose =

Here is the class:

    Private Class Dog

        Public Enum NoseType
            wet
            dry
            dirty
        End Enum

        Private dogNoseType As Dog.NoseType

        Public Property MyDogsNose() As Dog.NoseType
            Get
                Return dogNoseType
            End Get
            Set(ByVal value As Dog.NoseType)
                dogNoseType = value
            End Set
        End Property

    End Class
0
 
ckanoabAuthor Commented:
And the values of those types? True translates to -1 and such, is that limited to numbers? Can it be a string?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
They must be numbers.  By default they start at 0 and increment by one for each entry.

You can specify what you want them to be manually though:

    Private Class Dog

        Public Enum NoseType
            wet = 0
            dry = 2
            dirty = 4
        End Enum

        Private dogNoseType As Dog.NoseType

        Public Property MyDogsNose() As Dog.NoseType
            Get
                Return dogNoseType
            End Get
            Set(ByVal value As Dog.NoseType)
                dogNoseType = value
            End Set
        End Property

    End Class



Here are the two kinds of output you can get:

        Dim d As New Dog
        d.MyDogsNose = Dog.NoseType.dry
        Debug.Print(d.MyDogsNose) ' prints "2"
        Debug.Print(d.MyDogsNose.ToString) ' prints "dry"


Usually it helps with readability though:

        Dim d As New Dog
        d.MyDogsNose = Dog.NoseType.dry

        Select Case d.MyDogsNose
            Case Dog.NoseType.wet

            Case Dog.NoseType.dry

            Case Dog.NoseType.dirty

        End Select
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

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