如何循环遍历 VB.Net 数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26165397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I loop through a VB.Net array?
提问by Pr0no
I now do something inefficient like this:
我现在做这样低效的事情:
' Returns a collection of selected choices in a multichoice field
Dim MyField As NS.ChoiceValues = CType(Me.Form.Fields("Field").Value, NS.ChoiceValues)
If MyField.Choices.Item("Value 1") IsNot Nothing Then
' Do stuff to database choice Value 1, which has id 100
End If
If MyField.Choices.Item("Value 1") Is Nothing Then
' Do other stuff to database choice Value 1, which has id 100
End If
If MyField.Choices.Item("Value 2") IsNot Nothing Then
' Do stuff to database choice Value 2, which has id 200
End If
If MyField.Choices.Item("Value 2") Is Nothing Then
' Do other stuff to database choice Value 2, which has id 200
End If
...
This is very inefficient and becomes unreadable when the number of choice values increase. So I am trying to update this:
This is very inefficient and becomes unreadable when the number of choice values increase. 所以我想更新这个:
Dim Field1Choices As New Dictionary(Of Integer, String) From {
{100, "Value 1"},
{200, "Value 2"},
{300, "Value 3"},
{400, "Value 4"}
...
}
For Each FieldChoice As String In Field1Choices
If MyField.Choices.Item(var_a) ' var_a should be "Value 1", "Value 2", etc.
DoStuff.WithCoice(Me.Database, "SomeTable", var_b) 'var_b should be 100, 200 etc.
End If
Next
Obviously, this does not work. Because My array contains both integers and strings, For Each FieldChoice As String In Field1Choicesdoes not work.
显然,这行不通。因为我的数组同时包含整数和字符串,For Each FieldChoice As String In Field1Choices所以不起作用。
How can I loop through the Field1Choices array so that var_aand var_bget get the values of the array values?
我如何遍历数组Field1Choices让var_a和var_b获取Get数组值的值?
回答by Steve
Each entry in a Dictionary is returned as KeyValuePairtype that has the property Valueand the property Key.
In a For Each loop you don't need to declare the type of the iterator. It is identified correctly by the compiler looking at the type enumerated. In this case your Dictionary has an Integer key and a string value. So your KeyValuePairiterator contains a Key of type Integer and a Value of type string for every entry in the dictionary
Dictionary 中的每个条目都作为KeyValuePair类型返回,该类型具有属性Value和属性Key。
在 For Each 循环中,您不需要声明迭代器的类型。编译器查看枚举的类型可以正确识别它。在这种情况下,您的 Dictionary 有一个 Integer 键和一个字符串值。所以你的KeyValuePair迭代器包含一个整数类型的键和一个字符串类型的值,用于字典中的每个条目
Dim Field1Choices As New Dictionary(Of Integer, String) From {
{100, "Value 1"},
{200, "Value 2"},
{300, "Value 3"},
{400, "Value 4"}
}
For Each FieldChoice In Field1Choices
Dim var_A = FieldChoice.Value
Console.WriteLine(var_A)
DIm var_B = FieldChoice.Key
Console.WriteLine(var_B)
'DoStuff.WithCoice(Me.Database, "SomeTable", var_B) 'var_b should be 100, 200 etc.
Next

