在 VB.NET 中动态创建变量

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

Dynamically create variables in VB.NET

vb.netdynamic

提问by Jonathan

I have been trying to figure this out for some time now and can't seem to figure out an answer to it. I don't see why this would be impossible. I am coding in VB.NET.

一段时间以来,我一直试图弄清楚这一点,但似乎无法找到答案。我不明白为什么这是不可能的。我在 VB.NET 中编码。

Here is my problem:I need to dynamically create variables and be able to reference them later on in the code.

这是我的问题:我需要动态创建变量并能够稍后在代码中引用它们。

More Details:The number of variables comes from some math run against user defined values. In this specific case I would like to just create integers, although I foresee down the road needing to be able to do this with any type of variable. It seems that my biggest problem is being able to name them in a unique way so that I would be able to reference them later on.

更多详细信息:变量的数量来自对用户定义值的一些数学运算。在这种特定情况下,我只想创建整数,尽管我预见未来需要能够使用任何类型的变量来做到这一点。似乎我最大的问题是能够以独特的方式命名它们,以便我以后能够引用它们。

Simple Example:Let's say I have a value of 10, of which I need to make variables for. I would like to run a loop to create these 10 integers. Later on in the code I will be referencing these 10 integers.

简单示例:假设我的值为 10,我需要为其创建变量。我想运行一个循环来创建这 10 个整数。稍后在代码中我将引用这 10 个整数。

It seems simple to me, and yet I can't figure it out. Any help would be greatly appreciated. Thanks in advance.

对我来说似乎很简单,但我无法弄清楚。任何帮助将不胜感激。提前致谢。

回答by Steven Doggart

The best way to do something like this is with the Dictionary(T)class. It is generic, so you can use it to store any type of objects. It allows you to easily store and retrieve code/value pairs. In your case, the "key" would be the variable name and the "value" would be the variable value. So for instance:

做这样的事情的最好方法是在Dictionary(T)课堂上。它是通用的,因此您可以使用它来存储任何类型的对象。它允许您轻松存储和检索代码/值对。在您的情况下,“键”将是变量名称,“值”将是变量值。所以例如:

Dim variables As New Dictionary(Of String, Integer)()
variables("MyDynamicVariable") = 10  ' Set the value of the "variable"
Dim value As Integer = variables("MyDynamicVariable")  ' Retrieve the value of the variable

回答by Kratz

You want to use a List

你想使用一个 List

Dim Numbers As New List(Of Integer)

For i As Integer = 0 To 9 
    Numbers.Add(0)
Next

The idea of creating a bunch of named variables on the fly is not something you are likely to see in any VB.Net program. If you have multiple items, you just store them in a list, array, or some other type of collection.

动态创建一堆命名变量的想法在任何 VB.Net 程序中都不可能看到。如果您有多个项目,您只需将它们存储在列表、数组或其他类型的集合中。

回答by Richard

'Dim an Array Dim xCount as Integer Dim myVar(xCount) as String

'Dim an Array Dim xCount as Integer Dim myVar(xCount) as String

AddButton Event . . . xCount += 1 myVar(xCount) = "String Value"

添加按钮事件。. . xCount += 1 myVar(xCount) = "字符串值"

'You will have to keep Track of what xCount Value is equal to to use. 'Typically could be an ID in A DataTable, with a string Meaning

'您必须跟踪 xCount 值等于使用的值。'通常可以是 A DataTable 中的一个 ID,带有一个字符串 含义