循环使用枚举数组的类

时间:2014-03-24 14:16:42

标签: vb.net

我有一个类,其中一个字段是枚举类型数组:

Public Enum InvalidEmailType
    Unkown
    TrailingSpace
    InnerSpace
    TrailingPeriod
    MissingAtSign
    MultipleAtSigns
    FakeEmail
    PhoneExtension
End Enum
Public Class CustomerClass
    Public CustomerName As String
    Public LDCCode As Integer
    Public LDCName As String
    Public LDCAccountNo As String    
    Public Commodity As String
    Public Email As String
    Public ValidEmail As Boolean
    Public Suggestion As String
    Public Errortype = [Enum].GetValues(GetType(InvalidEmailType))
    Public ErrorDescription As String
End Class

当我向Errortype数组添加值时,我需要编写一个ErrorDescription,以便用户轻松阅读。 ErrorDescription字段的工作方式类似于1个单词中所有潜在错误的摘要。

但首先我需要确定有多少InvalidEmailType.Unkown实例。

为此,我写了一个函数来计算未知数。

我的问题是如何循环遍历类中的数组并将其与枚举相匹配?

这是我到目前为止所尝试的:

Public Shared Function ErroTypeMessage(customer As CustomerClass) As String
        Dim Instances As Integer = 0
        For Each value In customer.Errortype
            If value. <> InvalidEmailType.Unkown Then 'here is the problem

            End If
        Next
End Function

更新。这是新功能。我仍然无法获得准确的匹配

  Public Shared Function ErroTypeMessage(customer As CustomerClass) As String
        Dim Instances As Integer = 0
        ErroTypeMessage = Nothing
        For Each value In customer.Errortype
            If value <> InvalidEmailType.Unkown Then 'does not match properly
                Instances = +1
            Else
                ErroTypeMessage = value.ToString
            End If
        Next
        If Instances > 1 Then
            ErroTypeMessage = "Multiple"
        ElseIf Instances = 1 Then
            'do nothing
        Else
            ErroTypeMessage = "Unkown"
        End If
    End Function

我已经解决了这个问题。

Public Shared Function ErroTypeMessage(customer As CustomerClass) As String
        Dim Instances As Integer = 0
        ErroTypeMessage = Nothing
        Dim temp As String
        For Each value In customer.Errortype
            temp = DirectCast([Enum].Parse(GetType(InvalidEmailType), value.ToString), InvalidEmailType)
            If temp <> InvalidEmailType.Unkown Then
                ErroTypeMessage = temp
                Instances += 1
            End If
        Next
        If Instances > 1 Then
            ErroTypeMessage = "Multiple"
        ElseIf Instances = 1 Then
            ErroTypeMessage = CType(ErroTypeMessage, InvalidEmailType).ToString()
        ElseIf Instances = 0 Then
            ErroTypeMessage = "Unkown"
        End If
    End Function

1 个答案:

答案 0 :(得分:0)

您的电子邮件错误变量可以是一个数组,但枚举也可以是一种标记类型,并且您可以组合错误:

<Flags>
Enum InvalidEmailType
    NoError = 0 
    Unkown = 1            ' not needed?
    TrailingSpace = 2
    InnerSpace = 4
    TrailingPeriod = 8
    MissingAtSign = 16
    MultipleAtSigns = 32
    FakeEmail = 64
    PhoneExtension = 128
End Enum

在课堂上:

Public Errortype As InvalidEmailType = InvalidEmailType.None

在验证电子邮件地址的代码中,使用OR枚举值来累积错误:

Errortype = Errortype Or InvalidEmailType.TrailingPeriod   ' example

要测试条件,请使用And:

If Errtype And InvalidEmailType.FakeEmail = InvalidEmailType.FakeEmail Then
   ' take action here
End if

我不确定你是如何发现未知的。