有一个带有字符串属性的类(usign vs 2005 here)
Private _foo As String
Public Property Foo() As String
Get
Return _foo
End Get
Set(ByVal value As String)
??
End Set
End Property
我想将Foo的值限制为一组值列表。 不幸的是这些值包括特殊字符: “酒吧/酒吧”,“smthing / smthing”等。 所以我不认为我可以使用简单的枚举
有什么想法吗?
答案 0 :(得分:0)
这样的事情有助于检查arraylist
允许的值,如果没有则返回错误。显然不是一个严格的枚举
Public Class footest
Private _foo As String
Private allowed_values As ArrayList
Public Sub New(ByVal text As String)
MyBase.New()
allowed_values.Add("bar/bar")
allowed_values.Add("smthing/smthing")
End Sub
Public Property Foo() As String
Get
Return _foo
End Get
Set(ByVal value As String)
If allowed_values.Contains(value) Then
_foo = value
Else
_foo = "NA#"
End If
End Set
End Property
End Class
或者您可以创建没有特殊字符的枚举,然后在您在New
sub
Private hashtable As Hashtable
Public Sub New(ByVal text As String)
MyBase.New()
hashtable.Add("barbar", "bar/bar")
hashtable.Add("smthingsmthing", "smthing/smthing")
End Sub
答案 1 :(得分:0)
在CodeProject中找到它,它允许我将带有特殊字符的枚举返回到控制台,并要求您创建3个类。为避免在每个新arraylists
创建新的foo
等,您需要更精细一些
控制台
Module Module1
Sub Main()
Dim x As New _foo
x.Foo = FooString.barbar
Console.WriteLine(x.Foo)
End Sub
End Module
foo类
Public Class _foo
Private _foo As String
Public Property Foo() As FooString
Get
Return _foo
End Get
Set(ByVal value As FooString)
_foo = value
End Set
End Property
End Class
输出特殊字符的自定义枚举
Public NotInheritable Class FooString
Inherits StringEnumeration(Of FooString)
''ADD YOUR ALLOWED VALUES HERE
Public Shared ReadOnly barbar As New FooString("Bar/Bar")
Public Shared ReadOnly smthingsmthing As New FooString("smthing/smthing")
Private Sub New(ByVal StringConstant As String)
MyBase.New(StringConstant)
End Sub
End Class
字符串枚举处理类
Public MustInherit Class StringEnumeration(Of TStringEnumeration _
As StringEnumeration(Of TStringEnumeration))
Implements IStringEnumeration
Private myString As String
Sub New(ByVal StringConstant As String)
myString = StringConstant
End Sub
#Region "Properties"
Public Class [Enum]
Public Shared Function GetValues() As String()
Dim myValues As New List(Of String)
For Each myFieldInfo As System.Reflection.FieldInfo _
In GetSharedFieldsInfo()
Dim myValue As StringEnumeration(Of TStringEnumeration) = _
CType(myFieldInfo.GetValue(Nothing), _
StringEnumeration(Of TStringEnumeration))
'Shared Fields use a Null object
myValues.Add(myValue)
Next
Return myValues.ToArray
End Function
Public Shared Function GetNames() As String()
Dim myNames As New List(Of String)
For Each myFieldInfo As System.Reflection.FieldInfo _
In GetSharedFieldsInfo()
myNames.Add(myFieldInfo.Name)
Next
Return myNames.ToArray
End Function
Public Shared Function GetName(ByVal myName As _
StringEnumeration(Of TStringEnumeration)) As String
Return myName
End Function
Public Shared Function isDefined(ByVal myName As String) As Boolean
If GetName(myName) Is Nothing Then Return False
Return True
End Function
Public Shared Function GetUnderlyingType() As Type
Return GetType(String)
End Function
Friend Shared Function GetSharedFieldsInfo() _
As System.Reflection.FieldInfo()
Return GetType(TStringEnumeration).GetFields
End Function
Friend Shared Function GetSharedFields() As _
StringEnumeration(Of TStringEnumeration)()
Dim myFields As New List(Of _
StringEnumeration(Of TStringEnumeration))
For Each myFieldInfo As System.Reflection.FieldInfo _
In GetSharedFieldsInfo()
Dim myField As StringEnumeration(Of TStringEnumeration) = _
CType(myFieldInfo.GetValue(Nothing), _
StringEnumeration(Of TStringEnumeration))
'Shared Fields use a Null object
myFields.Add(myField)
Next
Return myFields.ToArray
End Function
End Class
#End Region
#Region "Cast Operators"
'Downcast to String
Public Shared Widening Operator CType(ByVal myStringEnumeration _
As StringEnumeration(Of TStringEnumeration)) As String
If myStringEnumeration Is Nothing Then Return Nothing
Return myStringEnumeration.ToString
End Operator
'Upcast to StringEnumeration
Public Shared Widening Operator CType(ByVal myString As String) As _
StringEnumeration(Of TStringEnumeration)
For Each myElement As StringEnumeration(Of TStringEnumeration) In _
StringEnumeration(Of TStringEnumeration).Enum.GetSharedFields
'Found a Matching StringEnumeration - Return it
If myElement.ToString = myString Then Return myElement
Next
'Did not find a Match - return NOTHING
Return Nothing
End Operator
Overrides Function ToString() As String Implements IStringEnumeration.ToString
Return myString
End Function
#End Region
#Region "Concatenation Operators"
Public Shared Operator &(ByVal left As StringEnumeration(Of _
TStringEnumeration), ByVal right As StringEnumeration(Of _
TStringEnumeration)) As String
If left Is Nothing And right Is Nothing Then Return Nothing
If left Is Nothing Then Return right.ToString
If right Is Nothing Then Return left.ToString
Return left.ToString & right.ToString
End Operator
Public Shared Operator &(ByVal left As StringEnumeration(Of _
TStringEnumeration), ByVal right As IStringEnumeration) As String
If left Is Nothing And right Is Nothing Then Return Nothing
If left Is Nothing Then Return right.ToString
If right Is Nothing Then Return left.ToString
Return left.ToString & right.ToString
End Operator
#End Region
#Region "Operator Equals"
Public Shared Operator =(ByVal left As StringEnumeration(Of _
TStringEnumeration), ByVal right As _
StringEnumeration(Of TStringEnumeration)) As Boolean
If left Is Nothing Or right Is Nothing Then Return False
Return left.ToString.Equals(right.ToString)
End Operator
Public Overrides Function Equals(ByVal obj As Object) As Boolean
If TypeOf (obj) Is StringEnumeration(Of TStringEnumeration) Then
Return CType(obj, StringEnumeration(Of _
TStringEnumeration)).ToString = myString
ElseIf TypeOf (obj) Is String Then
Return CType(obj, String) = myString
End If
Return False
End Function
#End Region
#Region "Operator Not Equals"
Public Shared Operator <>(ByVal left As StringEnumeration(Of _
TStringEnumeration), ByVal right As StringEnumeration(Of _
TStringEnumeration)) As Boolean
Return Not left = right
End Operator
#End Region
End Class
'Base Interface without any Generics for StringEnumerations
Public Interface IStringEnumeration
Function ToString() As String
End Interface