Class property?

  • Thread starter Thread starter Able
  • Start date Start date
A

Able

Dear friends

In a class I have this property:

Private mstr_FirstName As String

Public Property FirstName() As String

Get

Return mstr_FirstName

End Get

Set(ByVal Value As String)

mstr_FirstName = Value

End Set

End Property

But I want the mstr_FirstName to be only one of three different optional
values, lets say "Joe", "Pete" or "Frank". Somebody who knows how to
implemet this task?



Regards Able
 
Public Enum FName As Integer
Joe = 1
Pete = 2
Frank = 3
End Enum

Private mstr_FirstName As FName

Public Property FirstName() As FName
Get
Return mstr_FirstName
End Get
Set(ByVal Value As FName)
mstr_FirstName = Value
End Set
End Property
 
Able,
If you are really paranoid, you can check the enum value.

(extending Scott's sample).

Public Property FirstName() As FName
Get
Return mstr_FirstName
End Get
Set(ByVal value As FName)
If Not System.Enum.IsDefined(GetType(FName), value) Then
Throw New System.ComponentModel.InvalidEnumArgumentException( _
"FirstName", value, GetType(FName))
End If
mstr_FirstName = Value
End Set
End Property

Of course if you need FirstName to be a string, you can simply check for
valid values in your Set method.
Public Property FirstName() As String
Get
Return mstr_FirstName
End Get
Set(ByVal value As String)

Select Case value
Case "Joe"
Case "Pete"
Case "Frank"
Case Else
Throw New ArgumentOutOfRangeException( _
"FirstName", value, "Invalid first name!")
End Select
mstr_FirstName = Value
End Set
End Property

Hope this helps
Jay
 
* "Able said:
Dear friends

In a class I have this property:

Private mstr_FirstName As String

Public Property FirstName() As String

Get

Return mstr_FirstName

End Get

Set(ByVal Value As String)

mstr_FirstName = Value

End Set

End Property

But I want the mstr_FirstName to be only one of three different optional
values, lets say "Joe", "Pete" or "Frank". Somebody who knows how to
implemet this task?

\\\
Public Enum FirstName
Joe
Peter
Frank
End Enum

Private m_TheFirstName As FirstName

Public Property TheFirstName() As FirstName
Get
Return m_TheFirstName
End Get
Set(ByVal Value As FirstName)
If [Enum].IsDefined(GetType(FirstName), Value) Then
m_TheFirstName = Value
Else

' Throw exception or something like that...
End If
End Set
End Property
///
 
Able said:
Dear friends

In a class I have this property:

Private mstr_FirstName As String

Public Property FirstName() As String

Get

Return mstr_FirstName

End Get

Set(ByVal Value As String)

mstr_FirstName = Value

End Set

End Property

But I want the mstr_FirstName to be only one of three different
optional values, lets say "Joe", "Pete" or "Frank". Somebody who
knows how to implemet this task?

Another version:
Class Firstname
Public Shared ReadOnly Joe As New Firstname
Public Shared ReadOnly Pete As New Firstname
Public Shared ReadOnly Frank As New Firstname

Private Sub New()
End Sub
End Class

or:
Class Firstname
Public Shared ReadOnly Joe As New Firstname("Joe")
Public Shared ReadOnly Pete As New Firstname("Pete")
Public Shared ReadOnly Frank As New Firstname("Frank")

Public ReadOnly Name As String

Private Sub New(ByVal Name As String)
Me.Name = Name
End Sub
End Class
 
Hi Able,

The next sentence more to the others than to you.
I cannot see the deeper meaning from Able, but this can be what I often call
OO madness.
But I did make some code, what would in my opinion fits the problem the
best.

Public Class Madness
Private names As String() = {"Joe", "Pete", "Frank"}
Private mName As String
Public Property FName() As String
Get
Return mName
End Get
Set(ByVal Value As String)
If DirectCast(names, IList).IndexOf(Value) = -1 Then
mName = ""
Else
mName = Value
End If
End Set
End Property
End Class

Cor
 
Exellente. My next question is how I determine what value is picked, lets
say in a form procedure?

Regards Able
 
Hi Able,

To get the numeric value associated with the Enum, just check the public
FirstName property value of the class instance. To get the string
representtation associated with the value use: Class.FirstName.ToString.

-Scott
 
Back
Top