如何在枚举中包含整数字符串?
我知道这是一个正确的枚举:
Private Enum Months
JANUARY = 1
FEBRUARY = 2
...
End Enum
但是,我想要一个枚举,其中字符串仅是整数。
示例:
Private Enum ColumnCounts
01 = 5
02 = 4
03 = 40
End Enum
01、02、03 都是字符串。但是,如果我输入“01”、[01] 或只是 01,它会告诉我它期望枚举结束,并且它不是有效的语句。有什么方法可以实现我所缺少的吗?我什至尝试过。 01.ToString(),但这不是一个选项。 :) 感谢任何和所有的帮助。谢谢。
编辑:
Public Class Part
Private Enum ColumnCounts
f01 = 39
End Enum
Public Shared Function ValidateColumns(ByRef lstFields As List(Of String)) As Boolean
For Each colCount In [Enum].GetValues(GetType(ColumnCounts))
If colCount = "f" + lstFields(1) Then
'Right here I need to compare the count of the list vs. the value of the enum.'
Return True
End If
Next
Return False
End Function
End Class
本质上,我不想把 f 放在那里,只是想做 01。调用它的方式是:
Select Case (nRecordType)
Case "01"
...
Case "02"
...
Case "03"
Return Part.ValidateColumns(_lstFields)
End Select
因为我没有创建它的实例,也没有调用构造函数,无法自动填充字典。我不想转换为整数,所以我不打算做一个数组。这是为了以防万一该值最终超过 99 并且下一个值将是 A0。我正在尝试考虑未来对此的简单更改以及向后兼容性。如果您需要更多解释,请告诉我。
编辑2:
这是我现在所做的,我认为它应该有效:
Public Class Part
Private Shared columnCounts As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)
Public Shared Function ValidateColumns(ByRef lstFiels As List(Of String)) As Boolean
InitializeColumnDictionary()
Return lstFields.Count = columnCounts(lstFields(1))
End Function
Private Shared Sub InitializeColumnDictionary()
columnCounts.Add("01", 39)
End Sub
End Class
我现在还不能测试它,所以我无法验证它是否会起作用我想要什么,但是当我构建它时它不会给我一个错误,所以我祈祷。
I know this is a proper enum:
Private Enum Months
JANUARY = 1
FEBRUARY = 2
...
End Enum
However, I want to have an enum where the string will solely be integers.
Example:
Private Enum ColumnCounts
01 = 5
02 = 4
03 = 40
End Enum
The 01, 02 and 03 are all strings. However, if I put "01", [01] or just 01, it tells me it expects the end of the Enum and that it isn't a valid statement. Is there any way to accomplish this that I'm missing? I even tried. 01.ToString(), but that wasn't an option. :) Any and all help is appreciated. Thanks.
Edit:
Public Class Part
Private Enum ColumnCounts
f01 = 39
End Enum
Public Shared Function ValidateColumns(ByRef lstFields As List(Of String)) As Boolean
For Each colCount In [Enum].GetValues(GetType(ColumnCounts))
If colCount = "f" + lstFields(1) Then
'Right here I need to compare the count of the list vs. the value of the enum.'
Return True
End If
Next
Return False
End Function
End Class
Essentially, I didn't want to put the f in there, just wanted to do 01. The way this will be called is:
Select Case (nRecordType)
Case "01"
...
Case "02"
...
Case "03"
Return Part.ValidateColumns(_lstFields)
End Select
Because I'm not making an instance of it and not calling a constructor, there's no way to autopopulate a Dictionary. And I don't want to convert to integer so I'm not going to do an array. That is just in case eventually the value gets above 99 and the next value would be A0. I'm trying to think of easy future changes to this and backwards compatability. If you need more explanations, let me know.
Edit 2:
This is what I've done now and I think it should work:
Public Class Part
Private Shared columnCounts As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)
Public Shared Function ValidateColumns(ByRef lstFiels As List(Of String)) As Boolean
InitializeColumnDictionary()
Return lstFields.Count = columnCounts(lstFields(1))
End Function
Private Shared Sub InitializeColumnDictionary()
columnCounts.Add("01", 39)
End Sub
End Class
I'm not at a point where I can test it right now so I can't verify that it's going to do what I want to, but it doesn't give me an error when I build it so I'm crossing my fingers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
单独的整数不是有效的标识符。
为什么要将 01 称为标识符?
在枚举元素前面加上一个字母,就完成了:
根据您的编辑进行编辑:
声明一个静态字典并在静态方法中填充它(如果它为空):
Integers alone are NOT valid identifiers.
Why would you want to call 01 an identifier?
Prefix the enum elements with a letter, and you're done:
edit as per your editing:
declare a static dictionary and populate it, if it's null, in your static method:
枚举元素是标识符;并且标识符必须以字母(或下划线)开头。所以,我很抱歉,但这不可能。
注意:更正了评论后的起始字符。
Enum elements are identifiers; and identifiers must start with a letter (or underscore). So, I am sorry, but this is not possible.
Note: corrected start characters after a comment.
您需要使用有效标识符 - 不能以数字开头。
You need to use valid identifiers - which cannot begin with a number.
抱歉,任何类型的标识符都不可能以整数开头。你可以这样做:
Sorry, it's not possible to let an identifier of any kind start with an integer. You could do:
对于您的枚举,您应该使用 Column01 作为标识符。稍后在代码中,如果不需要 01 部分,您可以随时调用 .ToString() 并修剪 Column 部分。
For your enum you should use Column01 for the identifier. Later in your code, if you don't need the 01 portion, you can always call .ToString() and trim the Column portion out.
$.02
$.02