For 循环为您创建新变量 - VB.Net

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20122300/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:55:09  来源:igfitidea点击:

For loop make new variables for you - VB.Net

vb.netfunctionvariablesloopsfor-loop

提问by Nick Riggi

I'm trying to make a for loop for will make a new variable for me, every step. I want something like this. What ever step I am on, say, x = 2, it Dims newVar2, if x = 3: Dim newVar3

我正在尝试创建一个 for 循环 for 将为我创建一个新变量,每一步。我想要这样的东西。我所做的任何一步,例如,x = 2,它都会使 newVar2 变暗,如果 x = 3:Dim newVar3

Is there any way to do that? I was hoping something like, Dim newVar & x would work, but of course now.

有没有办法做到这一点?我希望像 Dim newVar & x 这样的东西会起作用,但当然是现在。

I was trying to do it as an array, but I'm not sure how to do that, or ReDimming, so examples would be great!

我试图把它作为一个数组来做,但我不知道怎么做,或者 ReDimming,所以例子会很棒!

回答by Nunners

To create a collection of variable values inside a for loop, you should use a List(Of t)object or something similar (For Example Dictionary).

要在 for 循环内创建变量值的集合,您应该使用List(Of t)对象或类似的东西(例如Dictionary)。

To do this with a List(Of t)you can do the following :

要使用 a 执行此操作,List(Of t)您可以执行以下操作:

Dim varList As New List(Of Integer)

For i As Integer = 0 To 10
   varList.add(i)
Next

Or if you want to do it with the variable names you mentioned, try :

或者,如果您想使用您提到的变量名称进行操作,请尝试:

Dim varList As New List(Of String)

For i As Integer = 0 To 10
   varList.add("newVar" & i)
Next

To retrieve the value from a List use the following : Dim result As String = varList(0)

要从列表中检索值,请使用以下命令: Dim result As String = varList(0)

Alternatively you can use a Dictionary object to store Key/Valuepairs :

或者,您可以使用 Dictionary 对象来存储Key/Value对:

Dim varList As New Dictionary(Of String, Integer)

For i As Integer = 0 To 10
   Dim k As Integer = 0
   varList.add("newVar" & i, k)
Next

Becareful though as a Dictionary object can only contain unique Keys. To return the value find it as : Dim result As Integer = varList("newVar0")

不过要小心,因为 Dictionary 对象只能包含唯一的键。要返回值,将其查找为:Dim result As Integer = varList("newVar0")

回答by Douglas Barbin

The other answers are technically correct, but you probably don't need the iteration loops. You can probably do this in a single line of code:

其他答案在技术上是正确的,但您可能不需要迭代循环。您可能可以在一行代码中执行此操作:

Dim varList As Dictionary(Of String, String) = Enumerable.Range(0, 10).ToDictionary(Function(k) "newVar" & k, Function(v) "Some value for newVar" & v)

回答by dovid

basic array:

基本数组:

 Dim Arr(1000) As Integer
 For i As Integer = 0 To 999
     Arr(i) = YYYYY
 Next

trick for dynamic size:

动态大小的技巧:

Dim max As Integer = XXXXXX
Dim Arr(1000) As Integer
For i As Integer = 0 To max
    'if too small, Increasing array without loss old data
    If Arr.Length = i Then
        ReDim Preserve Arr(Arr.Length + 1000)
    End If

    Arr(i) = YYYYY
Next

or, use list:

或者,使用列表:

Dim max As Integer = XXXXXX
Dim list As New List(Of Integer)
For i As Integer = 0 To max
    list.Add(YYYYY)
Next

回答by j.i.h.

Dim myInts As New List(Of Int)

For i = 0 To 10
    myInts.Add(i)
Next