2017-05-17 61 views
0

有人檢查我的代碼類,如果可能這樣做,我得到了類內的值。詳細如下檢查:類與數據結構參數類C#團結

classLogBarter.cs //使類

[System.Serializable] 
public class classLogBarter { 
    public string DisplayName; 
    public string PlayerID; 
    public int CommentID; 
    public string Time; 
    public List<item> ItemBarter = new List<item>(); // Here is that possible ? 

    public classLogBarter (string playerid, string displayname, int commentid, string time, List<item> itembarter // Here how about this ?) { 
     PlayerID = playerid; 
     DisplayName = displayname; 
     CommentID = commentid; 
     Time = time; 
     itembarter = new List<item>(); // Is this correct ? 
     ItemBarter = itembarter; // Is this correct ? 
    } 

    public classLogBarter Clone() { 
     return new classLogBarter (PlayerID, DisplayName, CommentID, Time, ItemBarter); 

    } 

    public classLogBarter() { 

    } 
} 

LogBarter.cs //添加值到類// Asumption是BarterItem變量有1或2的值。

public List<classLogBarter> LogBarter = new List<classLogBarter>(); 
public List<item> BarterItem = new List<item>(); // Asumption that BarterItem Have 1 or 2 Value. Here now have BarterItem[0] and BarterItem[1] 

LogBarter.Add(new classLogBarter(playerID,displayNames,1,times,BarterItem)); 

//向的debug.log顯示LogBarter值

for (int i = 0; i < LogBarter.Count; i++) { 
    for(int j = 0; j < LogBarter[i].ItemBarter.Count; j++) { 
        Debug.Log ("Player LogBarter : " + LogBarter [i].ItemBarter [j].itemName); // Here there is no value in here. 

       } 
} 

我要問什麼是爲什麼當LogBarter的debug.log [I] .ItemBarter [J] .itemName有沒有物品?

有什麼我想念的嗎?或者我只是在用paramater類創建類時犯了錯誤?

+0

什麼意思是「假設有1或2個值」?爲什麼你認爲它有價值?你在某處添加它們嗎?如果這是你的所有代碼,你有什麼是準備添加項目的列表,但沒有項目。 – Everts

+0

是的,我已經在LogBarter.Add之前添加它們。所以我假設BarterItem已經有了2個值。你現在明白了嗎? –

+0

你能檢查課堂上課時我是如何放置課堂項目的嗎?這是對的還是不對? –

回答

1

錯誤位於構造函數的classLogBarter

您將要覆蓋有那些2個值你沒有得到的值:

itembarter = new List<item>(); // Is this correct ? 

刪除了這一行,你應該得到的項目,而不是空了。

您正在創建一個新的空的List<item>,但您已經完成並填充了(您所說的)2個值,但是當輸入構造函數時,您將分配一個新的空變量以保存已填充的變量。

+1

謝謝也許這是問題..我用空值覆蓋.. –