0

好的,这可能是不可能的,但我想使用一个字符串作为函数的参数,并使用该字符串来命名一个类的新实例。使用带有 C# XNA 的 Visual Studios 2010,我尝试使用 google 和 here 搜索解决方案,但也许我没有使用正确的关键字或其他东西。

     public void createRace(string raceName, Element element1, Element element2, AbilityScore stat1, AbilityScore stat2, AbilityScore stat3, AbilityScore stat4)
    {
        Race temp = new Race();
        temp.raceName = raceName;
        temp.primaryElement = element1;
        temp.secondaryElement = element2;
        temp.primaryAbility = stat1;
        temp.secondaryAbility1 = stat2;
        temp.secondaryAbility2 = stat3;
        temp.weakAbility = stat4;
    }

我希望 Race temp 在命名 Race 的新实例时使用 raceName 而不是 temp,如果不可能,请告诉我,我会找到解决方法。

4

1 回答 1

1

我会用字典来解决这个问题。以下代码将允许您基于“raceName”访问您的实例

Dictionary<string,Race> races = new Dictionary<string,Race>();

  public void createRace(string raceName, Element element1, Element element2, AbilityScore stat1, AbilityScore stat2, AbilityScore stat3, AbilityScore stat4)
    {
        Race temp = new Race();
        temp.raceName = raceName;
        temp.primaryElement = element1;
        temp.secondaryElement = element2;
        temp.primaryAbility = stat1;
        temp.secondaryAbility1 = stat2;
        temp.secondaryAbility2 = stat3;
        temp.weakAbility = stat4;

        races.Add(raceName,temp);
    }
于 2013-06-13T03:23:36.270 回答