如何在运行时创建一系列编号的变量? [英] How can I create a sequence of numbered variables at run time?

查看:845
本文介绍了如何在运行时创建一系列编号的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友,我必须创建一系列 ArrayList s,每个都包含未知来源的对象,每个实例分配给一个单独的局部变量中。



到目前为止,很好...但我还需要每个局部变量的名称遵循一个非常特定的模式:名称应以oArr开头,后跟一个或更多数字反映特定阵列在序列内的位置。此外,我不会在编译时知道有多少个这样的数组,因此,我将需要多少个局部变量!



这让我想起这是也许一个问题,可以通过在C#4.0中的动态类型的可用性来解决,但是我不是很熟悉他们的使用。我可以如何采取这样的代码...

  int i = 0; 
foreach(something)
{
ArrayList oArr + i = new ArrayList();
i ++;
}

...并将其转换为符合上述条件的内容
h2_lin>解决方案

由于代码(甚至c#代码)是使用某个变量名编译的,因此在执行期间不能更改变量的名称。如果你可以在执行过程中改变名称,那么它会导致问题。



例如,如果允许更改变量名的语言,那么当您尝试访问名为var1的变量时,编译器不知道执行期间变量名是否已更改现在称为'x'。



你可以尝试做的是允许你的程序动态编译一些代码,但这可能不是你的问题的正确解决方案。



希望这有帮助



编辑:查看您的版本我可以告诉您,使用当前使用的方法是不可能的。我可以建议你以下:

  int i = 0; 
List< ArrayList> masterList = new List< ArrayList>();
foreach(something)
{
masterList.Add(new ArrayList());
i ++;
}



如果你需要的是让每个ArrayList有一个特定的名称,你可以回调您可以使用字典:

  int i = 0; 
字典< string,ArrayList> masterList = new Dictionary< string,ArrayList>();
foreach(something)
{
masterList.Add(oArr+ i.ToString(),new ArrayList());
i ++;
}
ArrayList al = masterList [oArr1];


Friends, I must create a series of ArrayLists, each containing objects of unknown origin, with each instance assigned to a separate local variable.

So far, so good... But I also need each local variable's name to follow a very specific pattern: the name should begin with "oArr", followed by one or more digits reflecting that particular array's position within the sequence. Furthermore, I will not know at compile-time how many of these arrays - and hence, how many local variables - I will be needing!

It strikes me that this is perhaps a problem that could be solved by the availability of dynamic types in C# 4.0, however I am not at all familiar with their use. How might I take code like this...

int i=0;
foreach(something)
{
    ArrayList oArr+i=new ArrayList();
    i++;
}

...and turn it into something that matches the criteria outlined above and actually compiles?

Alternately, is there a more simple, sane approach to this problem?

解决方案

You cannot change the name of a variable during execution, since the code (even c# code) was compiled with a certain variable name. If you could change the name during execution then it would cause problems.

For example, if the language allowed to change variable names then when you try to access a variable named 'var1' the compiler has no idea if during execution that variable name changed and now is called 'x'.

Something you could try to do is to allow your program to dynamically compile some code but this is probably not the right solution to your problem. If you explain better what you need then we could provide you with an effective solution.

Hope this helped

EDIT: Seeing your editions I can tell you that it is impossible with the approach you are currently using. I could suggest you the following:

int i = 0;
List<ArrayList> masterList = new List<ArrayList>();
foreach (something)
{
     masterList.Add(new ArrayList());
     i++;
}

If what you need is to have each ArrayList to have a specific name you can recall you can use a dictionary:

int i = 0;
Dictionary<string, ArrayList> masterList = new Dictionary<string, ArrayList>();
foreach (something)
{
     masterList.Add("oArr" + i.ToString(), new ArrayList());
     i++;
}
ArrayList al = masterList["oArr1"];

这篇关于如何在运行时创建一系列编号的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆