發現創建一個哈希表/排序列表/字典這在CodeProject,它允許我返回一個帶有特殊字符的枚舉到控制檯,並要求你創建3個類。爲了避免在每個新foo
創造新arraylists
等你需要一點更復雜
控制檯
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
一個String枚舉處理類
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
這個工作,但如果創建這是什麼類的實例數百幾千倍?這意味着對於「footest」類的每個實例,都會創建另一個arrayList。 – GlutVonSmark