具有数据结构参数的类C#Unity

具有数据结构参数的类C#Unity,c#,unity3d,C#,Unity3d,有人可以检查我的代码类吗?如果可以这样做,我在类值内得到null。有关详细信息,请参见下文: classLogBarter.cs//创建一个类 [System.Serializable] public class classLogBarter { public string DisplayName; public string PlayerID; public int CommentID; public string Time; public List&l

有人可以检查我的代码类吗?如果可以这样做,我在类值内得到null。有关详细信息,请参见下文:

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() {

    }
}
[System.Serializable]
公共类类逻辑交换{
公共字符串显示名;
公共字符串播放器ID;
公共ID;
公共字符串时间;
public List ItemBarter=new List();//这是可能的吗?
public-classLogBarter(字符串playerid、字符串displayname、int-commentid、字符串时间、列表项barter//这里这个怎么样?){
PlayerID=PlayerID;
DisplayName=DisplayName;
CommentID=CommentID;
时间=时间;
itembarter=new List();//这是否正确?
ItemBarter=ItemBarter;//这是否正确?
}
公共类logbarter克隆(){
返回新的classLogBarter(PlayerID、DisplayName、CommentID、Time、ItemBarter);
}
公共类logbarter(){
}
}
LogBarter.cs//向类添加值//假定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));
public List LogBarter=new List();
公共列表BarterItem=新列表();//假设BarterItem有1或2个值。现在这里有BarterItem[0]和BarterItem[1]
添加(新类LogBarter(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.

                }
}
for(int i=0;i
我想问的是为什么Debug.Log LogBarter[i].ItemBarter[j].itemName没有项


有什么我错过的吗?或者我只是在如何使用paramater类创建类时犯了一个错误?

错误在构造函数上的
classLogBarter

您正在覆盖包含以下两个未获取值的值:

itembarter=新列表();//这是正确的吗?
删除该行,您现在应该获得项目,并且不为null


您正在创建一个新的空
列表
,但是您已经这样做了,并用(您说的)2个值填充了它,但是当进入构造函数时,您正在将一个新的空列表分配给保存已填充列表的同一个变量。

什么是“假设有1或2个值”?为什么你认为它有价值?你要把它们加在什么地方吗?如果这是您的全部代码,那么您拥有的是一个准备添加项目的列表,但其中没有项目。是的,我在LogBarter.add之前添加了项目。所以我假设BarterItem在类中已经有2个值。你现在明白了吗?你能检查一下我是如何在课堂上放置课堂物品的吗。对不对?
itembarter = new List<item> (); // Is this correct ?