2014-03-24 19 views
0

我有一个类,其中字段中的一个是枚举类型数组:回路通过与枚举阵列的类

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

问题是什么?发布错误,如果有的话。 –

+0

错误类型是否可以多次出现? –

+0

大部分时间ErrorType将是未知的。 这个数组目前持有大约10个值,其中90%将是未知的。但其中一些会有2个值不是未知的,为此我需要编写一个汇总错误描述 –

回答

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 

在你的代码来验证电子邮件阿迪,或枚举值累积的错误:

Errortype = Errortype Or InvalidEmailType.TrailingPeriod ' example 

要测试条件,使用和:

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

我不知道你是如何检测未知。