将可选的枚举传递给VB.NET中的函数

时间:2015-09-16 14:05:15

标签: vb.net enums optional-parameters enumerable

我正在尝试将可选的Enum传递给函数,但无法正确获取语法。我正在尝试以下方法:

Public Function MyFunction(ByVal TestCode As String, Optional ByVal Columns As [Enum] = MyEnumName.EnumVariable) As Boolean

也尝试过:

Public Function MyFunction(ByVal TestCode As String, Optional ByVal Columns As [Enum] = MyEnumName) As Boolean

并收到错误消息

“从”MyEnumName“到”System.Enum“的转换不能出现在常量表达式中”

是否可以使用可选的枚举或我做错了什么?

提前致谢

1 个答案:

答案 0 :(得分:3)

在处理Enum时,您必须指定给定名称而不是[Enum]作为类型。示例代码:

Public Enum MyEnumName
    val1
    val2
End Enum

Public Function MyFunction(ByVal TestCode As String, Optional ByVal Columns As MyEnumName = MyEnumName.val1) As Boolean

End Function