2016-11-30 46 views
1

我創建了一個屬性爲String的對象,另一個是List<String>。 我也創建了一個static List<MyObject>,我添加了所有的對象。 現在我的問題是第二個屬性被覆蓋。自己的類對象屬性被取代

比如我有3個對象:

Object1: "Name"; List with 3 Strings 
Object2: "Name2"; List with 2 Strings 
Object3: "Name3"; List with 5 Strings 

如果我現在將它們添加到我的對象列表,它看起來像這樣

Name; List with 5 Strings 
Name2; List with 5 Strings 
Name3; List with 5 Strings 

它重寫第二個屬性中的所有其他對象列表。

代碼:

for (int i = 0; i < 100; i++) 
{ 
    if (elo.ReadObjMask(i) > 0) 
    { 
     var iRet = elo.PrepareObjectEx(0, 0, i); 
     maskenname = elo.ObjMName(); 
     if (maskenname != "") 
     { 
      for (int e = 0; e < 50; e++) 
      { 
       string eigenschaft = elo.GetObjAttribName(e); 

       if (eigenschaft != "" && eigenschaft != "-") 
       { 
        eigenschaften.Add(eigenschaft); 
       } 
      } 
      allMasks.Add(maskenname); 

     } 
     else 
     { 
      // Do nothing 
     } 
     EloMask emask = new EloMask(maskenname, eigenschaften); 
     staticVariables.allMask.Add(emask); 
     eigenschaften.Clear(); 
    } 
} 

這裏是我的對象類:

public class EloMask 
{ 
    public string name; 
    public List<String> eigenschaften; 

    public EloMask(string iname, List<String> ieigenschaften) 
    { 
     name = iname; 
     eigenschaften = ieigenschaften; 
    } 
} 
+0

你需要指定你的「對象」類和你如何實例化後,加入到靜態列表中的代碼。 – Thangadurai

+0

staticVariables.allMask.Add(emask);在這部分我將它添加到我的靜態列表 – saamii

+0

我相信: EloMask emask = new EloMask(maskenname,eigenschaften); 傳遞eigenschaften作爲參考,所以當它被改變時,所有其他人。 您需要在寫入之前啓動列表的新實例。 eigenschaften =新列表; –

回答

1

List<string>總是指向同一個實例,因爲您傳遞的是對列表的引用,而不是副本。因此,該列表將被清除並再次填入您將該列表傳入的每個EloMask

爲了解決您的問題,創建一個新的列表,而不是:

if (elo.ReadObjMask(i) > 0) 
{ 
    var iRet = elo.PrepareObjectEx(0, 0, i); 
    maskenname = elo.ObjMName(); 

    // create a new list here!!! 
    var eigenschaften = new List<string>(); 

    if (maskenname != "") 
    { 
     for (int e = 0; e < 50; e++) 
     { 
      string eigenschaft = elo.GetObjAttribName(e); 

      if (eigenschaft != "" && eigenschaft != "-") 
      { 
       eigenschaften.Add(eigenschaft); 
      } 
     } 

     allMasks.Add(maskenname); 

    } 

    EloMask emask = new EloMask(maskenname, eigenschaften); 
    staticVariables.allMask.Add(emask); 

    // clearing the list is no longer needed     
} 
0

下面是一個例子,你如何能做到你想要什麼:

public static List<Person> PersonsList = new List<Person>(); 
    public static Random rd = new Random(); 
    static void Main(string[] args) 
    { 

     for (int i = 0; i < 5; i++) 
     { 
      List<string> tmpAbilities = new List<string>() {((char)rd.Next(255)).ToString(), ((char)rd.Next(255)).ToString() , ((char)rd.Next(255)).ToString() }; 
      Person tmpPerson = new Person("TmpName_"+i,tmpAbilities); 
      PersonsList.Add(tmpPerson); 
     } 
     foreach (var persona in PersonsList) 
     { 
      Console.WriteLine(persona); 
     } 
    } 


    public class Person 
    { 

     public string Name { get; set; } 

     public List<string> Abilities; 

     public Person(string name,List<string> abilities) 
     { 
      Name = name; 
      Abilities = abilities; 
     } 

     public override string ToString() 
     { 
      string retVal = $"Name: {Name}\n"; 
      foreach (var ability in Abilities) 
      { 
       retVal += $"Ability : {ability}\n"; 
      } 
      return retVal; 
     } 
    } 
0
 for (int i = 0; i < 100; i++) 
     { 

      if (elo.ReadObjMask(i) > 0) 
      { 
       // Create a new listin here 
       eigenschaften = new List<string>(); 

       var iRet = elo.PrepareObjectEx(0, 0, i); 
       maskenname = elo.ObjMName(); 
       if (maskenname != "") 
       { 
        for (int e = 0; e < 50; e++) 
        { 
         string eigenschaft = elo.GetObjAttribName(e); 

         if (eigenschaft != "" && eigenschaft != "-") 
         { 
          eigenschaften.Add(eigenschaft); 
         } 
        } 
        allMasks.Add(maskenname); 

       } 

       else 
       { 
        // Do nothing 
       } 
       EloMask emask = new EloMask(maskenname, eigenschaften); 
       staticVariables.allMask.Add(emask); 
      } 
     } 
+0

謝謝特洛伊!您在這裏缺少():eigenschaften =新列表; – saamii

+0

謝謝。匆匆太多;)只是更新了代碼。 –

+0

沒問題,我再次感謝你!我昨天掙扎了3個小時哈哈:D – saamii