2014-02-18 194 views
1

的有一個字符串屬性(usign VS 2005此處)一類字符串枚舉類有限制的價值觀和特殊字符

Private _foo As String 
Public Property Foo() As String 
    Get 
     Return _foo 
    End Get 
    Set(ByVal value As String) 
     ?? 
    End Set 
End Property 

我想富的價值觀restric到值的一組列表。 不幸的是這些值包含特殊字符: 「酒吧/條」,「smthing/smthing」等。 所以我不認爲我可以用一個簡單枚舉

任何想法?

回答

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

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 
+0

這個工作,但如果創建這是什麼類的實例數百幾千倍?這意味着對於「footest」類的每個實例,都會創建另一個arrayList。 – GlutVonSmark

0

發現創建一個哈希表/排序列表/字典這在CodeProject,它允許我返回一個帶有特殊字符的枚舉到控制檯,並要求你創建3個類。爲了避免在每個新foo創造新arraylists等你需要一點更復雜

enter image description here

控制檯

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 
+0

如果您希望節省空間並減少類的數量,您可以將'FooString'類添加到'_foo'類的底部 – TylerDurden

+0

是否解決了您的問題? – TylerDurden