2014-03-26 75 views
0

我想比较一个枚举数组与单个枚举实例。将枚举数组或列表传递给函数

介绍

我有一个枚举类型数组\目录

Public Enum InvalidEmailType 
    Multiple_Updates 
    Period_Before_At_Sign 
    Missing_Dot_Com 
End Enum 
Public Class CustomerClass 
    Public CustomerName As String 
    Public ErrorTypeList = [Enum].GetValues(GetType(InvalidEmailType)) 
    Public ErrorDescription As String 
End Class 

根据被添加到列表中什么样的价值观一类,我想运行特定的代码。

为了做到这一点,我的整个列表比较单一实例:

 If UpdateCustomer.MatchErrorType(customer.ErrorTypeList, InvalidEmailType.Trailing_Period) = True Then 
      'Run Code 
     End If 

功能里面我比较反对单一实例的整个列表。

换句话说,我通过类里面的整个列表循环,并检查值有:

Public Shared Function MatchErrorType(CustomerErrortypeList As List(Of InvalidEmailType), EmailError As InvalidEmailType) As Boolean 
    MatchErrorType = False 
    Dim Found As InvalidEmailType = CustomerErrortypeList.Where(Function(match) match.ToString = EmailError.ToString).OrderByDescending(Function(match) match.ToString).FirstOrDefault() 
    If Found > 0 Then 
     MatchErrorType = True 
    End If 
End Function 

这里的问题是: 如何声明数组\列表中函数参数?

名单(共InvalidEmailType)不工作,因为我得到一个错误投

无法投类型的对象EmailValidationReport.InvalidEmailType []'键入“System.Collections.Generic.List`1 [ EmailValidationReport.InvalidEmailType]'

回答

1

将ErrorTypeList设置为List(of InvalidEmailType)而不是数组。

Public ErrorTypeList = [Enum].GetValues(GetType(InvalidEmailType)) _ 
         .Cast(of InvalidEmailType)().ToList() 

Dim list = customer.ErrorTypeList.Cast(of InvalidEmailType)().ToList() 
If UpdateCustomer.MatchErrorType(list, InvalidEmailType.Trailing_Period) Then 
    'Run Code 
End If 
+0

该列表包含在客户类 –

+0

@InternetEngineer好的,你可以在你调用该函数之前做到这一点,查看更新的答案。 – Magnus

+0

演员表不起作用: 找不到类型为'InvalidEmailType()'的公共成员'Cast' –

1

既然你没有做任何特定于ListArray,你可以让你的方法签名参加一个IEnumerable而不是List。这应该能够处理ListArray(以及其他几种类型)。

Public Shared Function MatchErrorType(CustomerErrortypeList As IEnumerable(Of InvalidEmailType), EmailError As InvalidEmailType) As Boolean 
    Dim Found As InvalidEmailType = CustomerErrortypeList.Where(Function(match) match.ToString = EmailError.ToString).OrderByDescending(Function(match) match.ToString).FirstOrDefault() 
    MatchErrorType = (Found > 0) 
End Function