12

我有一个要循环的字符串数组。我想遍历数组并在每次迭代时创建一个名称与字符串值匹配的新对象。

例如;

string[] array = new string[] { "one", "two", "three" };

class myClass(){

    public myClass(){
    }
}

foreach (string name in array)
{
   myClass *value of name here* = new myClass(); 
}

将导致实例化三个对象,名称分别为“一”、“二”和“三”。

这是可能的还是有更好的解决方案?

4

9 回答 9

12

您尝试做的事情在静态类型语言中是不可能的。IIRC,这在 PHP 上是可能的,但这是不可取的。

改用字典:http: //ideone.com/vChWD

using System;
using System.Collections.Generic;

class myClass{

    public string Name { get; set; }
    public myClass(){
    }
}

class MainClass
{

    public static void Main() 
    {
        string[] array = new string[] { "one", "two", "three" };
        IDictionary<string,myClass> col= new Dictionary<string,myClass>();
        foreach (string name in array)
        {
              col[name] = new myClass { Name = "hahah " + name  + "!"};
        }

        foreach(var x in col.Values)
        {
              Console.WriteLine(x.Name);
        }

        Console.WriteLine("Test");
        Console.WriteLine(col["two"].Name);
    }
}

输出:

hahah one!
hahah two!
hahah three!
Test
hahah two!
于 2012-06-07T11:27:17.310 回答
5

虽然其他人给了你一个替代品,但没有人告诉他们为什么推荐你。

那是因为您无法使用动态名称访问对象。

深思熟虑:想一想,如果你能这样做,你将如何在它们被编码/命名之前访问它们。)

Dictionary<string, myClass>而是像其他人提到的那样创建一个。

于 2012-06-07T11:17:09.590 回答
4

使用 aDictionary<String, myClass>代替:

var dict= new Dictionary<String, myClass>();

foreach (string name in array)
{
    dict.Add(name, new myClass());
}

现在您可以myClass通过您的名称访问实例:

var one = dict["one"];

或循环:

foreach (string name in array)
{
    myClass m = dict[ name ];
}
于 2012-06-07T11:17:52.587 回答
1

您可以使用这种方法:

  var array = [srt1, srt2, str3];
  var other_array = [];

  for(var i = 0; i <  array.length; i++){
    other_array.push({
    name: array[i]
    })
  }

对于查找,很容易通过过滤找到所需的实例:

var instance1 = other_array.filter(function(result) {
return result.name == 'str1';
});
于 2018-06-22T15:36:46.613 回答
0

看起来您需要一个对象字典列表:

var myClassDictionary = new Dictionary<string,myClass>();

foreach (string name in array)
{
  myClassDicationry.Add(name, new myClass());
}

// usage:
// myClass["one"] <- an instance of myClass

据我所知,没有一种编程语言可以让您在运行时定义变量名。

于 2012-06-07T11:18:59.513 回答
0

你可以做这样的事情 -

Dictionary<string, myClass> classes = new Dictionary<string, myClass>();

foreach(string name in array)
{
    classes[name] = new myClass();
}

然后您可以稍后参考命名实例

classes[name].MyClassMethod();
于 2012-06-07T11:19:10.087 回答
0

您可以使用以下代码。

string[] array = new string[] { "one", "two", "three" };
Dictionary<String, myClass> list;
class myClass(){

   public myClass(){
   list = new Dictionary<String, myClass>();
   }
 } 

 foreach (string name in array)
 {
    list.Add(name, new myClass())
 }
于 2012-06-07T11:19:18.070 回答
0

您可以使用列表来存储对象,以便您可以访问它们

list<myClass> myClasses = new List<myClass>();
foreach (myClass object in myClasses)
{
    //preform interaction with your classes here 
}
于 2012-06-07T11:20:31.710 回答
0

不适用于 C# 或任何静态类型语言。

出于好奇,我尝试了我在 PHP 中记住的内容(动态创建变量)是否仍然正确。

它仍然是相同的 PHP,我上次使用它是在 2000 年。您可以即时生成变量,但并不是说它是可取的,它会污染全局变量,它可能会破坏一些现有的变量或同名对象。

https://ideone.com/nJDiou

<?php

class MyClass
{
    private $v;
    function __construct($x) {
        $this->v = $x;
    }
    public function getValue() {
        return $this->v;
    }
}

$one = new MyClass("I'm tough!");

echo "The one: " . $one->getValue() . "\n";

$i = 0;
foreach(array("one","two","three") as $h) {
   $$h = new MyClass("Says who? " . ++$i);
}

echo "The one: " . $one->getValue() . "\n";

echo $two->getValue() . "\n";
echo $three->getValue() . "\n";

echo "loop\n";

foreach(array("three","one","two") as $h) {
   echo $$h->getValue() . "\n";
}

?>

输出:

The one: I'm tough!
The one: Says who? 1
Says who? 2
Says who? 3
loop
Says who? 3
Says who? 1
Says who? 2
于 2012-06-07T11:42:03.370 回答