-3

皆さん、一連の を作成する必要があります。それぞれのオブジェクトには不明な起源のオブジェクトがArrayList含まれており、各インスタンスは個別のローカル変数に割り当てられています。

これまでのところ、とても良いです...しかし、各ローカル変数の名前も非常に特定のパターンに従う必要があります。名前は「oArr」で始まり、シーケンス内の特定の配列の位置を反映する1つ以上の数字が続きます。さらに、コンパイル時に、これらの配列がいくつ必要になるか、したがってローカル変数がいくつ必要になるかわかりません。

これはおそらく、C# 4.0 で動的型を使用できるようになったことで解決できる問題だと思いますが、私はそれらの使用法にまったく慣れていません。このようなコードをどのように取ることができますか...

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

...そして、上記の基準に一致し実際にコンパイルされるものに変換しますか?

あるいは、この問題に対するより単純で健全なアプローチはありますか?

4

3 に答える 3

6

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"];
于 2010-07-01T05:07:09.367 に答える
4

これはうまくいきますか?

var arrayLists = new List<ArrayList>();
var i = 0;
foreach(var item in list)
{
    arrayLists.Add(new ArrayList());
    i++;
}

次に、各配列リストにインデックスでアクセスできます。

于 2010-07-01T05:14:45.143 に答える
1

ArrayList のリストを使用します。

于 2010-07-01T05:17:02.127 に答える