public class User
{
private string name;
private string addressLine1;
private string addressLine2;
private string city;
private string zip;
private string state;
private string emailAddress;
private string password;
private string loginpassword;
private SoldBook[] soldBooks;
public string Name
{
get { return name; }
set { name = value; }
}
public string AddressLine1
{
get { return addressLine1; }
set { addressLine1 = value; }
}
public string AddressLine2
{
get { return addressLine2; }
set { addressLine2 = value; }
}
public string City
{
get { return city; }
set { city = value; }
}
public string Zip
{
get { return zip; }
set { zip = value; }
}
public string State
{
get { return state; }
set { state = value; }
}
public string EmailAddress
{
get { return emailAddress; }
set { emailAddress = value; }
}
public string Password
{
get { return password; }
set { password = value; }
}
public string LoginPassword
{
get { return loginpassword; }
set { loginpassword = value; }
}
public SoldBook[] SoldBooks
{
get { return soldBooks; }
set { soldBooks = value; }
}
}
public class SoldBook
{
public string ISBNNumber
{
get { return isbnnumber; }
set { isbnnumber = value; }
}
public string Title
{
get { return title; }
set { title = value; }
}
}
现在,我正在创建对象
user user1 = new User;
SoldBook sold = new SoldBook();
sold.ISBNNumber ="value";
sold.Title = "value";
现在我想在用户对象的SoldBook数组中添加售出的对象。
我想做user1.SoldBooks.add(sold);
但这不对。在用户对象的数组SoldBook中添加售出对象的正确方法是什么
答案 0 :(得分:0)
尝试使用如下List<>
。
public class User
{
private List<SoldBook> soldBooks;
public List<SoldBook> SoldBooks
{
get { return soldBooks; }
set { soldBooks = value; }
}
user user1 = new User;
SoldBook sold = new SoldBook();
sold.ISBNNumber ="value";
sold.Title = "value";
user1.SoldBooks.add(sold);