Need help for c# coding
This is my class structure
public class Customer
{
List<Address> add = null;
public Customer()
{
add = new List<Address>();
}
public int EmpID { get; set; }
public string Name { get; set; }
public List<Address> Address
{
get { return add; }
set { add = value; }
}
public double Salary { get; set; }
}
public class Address
{
public string Address1 { get; set; }
public string Address2 { get; set; }
public string PostCode { get; set; }
}
public void Populate()
{
List<Customer> oCust = new List<Customer>()
{
new Customer() { EmpID=1, Name="Sonia"},
new Customer() { EmpID=2, Name="Bill"},
new Customer() { EmpID=3, Name="Mark"},
};
}
from the populate method i am populating customer along with address.
when i am populating customer then how to populate address along with it
i am not a advance user so please guide me regarding code th开发者_运维问答at how
new Customer() { EmpID=3, Name="Mark" new Address{}}
,
please help....thanks
List<Customer> oCust = new List<Customer>()
{
new Customer()
{
EmpID = 1,
Name = "Sonia",
Address =
{
new Address { Address1 = "foo", Address2 = "bar", PostCode = "pc" },
new Address { Address1 = "foo 2", Address2 = "bar 2", PostCode = "pc 2" },
}
},
new Customer()
{
EmpID = 2,
Name = "Bill",
Address =
{
new Address { Address1 = "bill 1", Address2 = "bill 2", PostCode = "bill pc" },
}
}
};
Use a collection initializer:
List<Customer> oCust = new List<Customer>()
{
new Customer()
{
EmpID=1,
Name="Sonia",
Address =
{
new Address { Address1 = "addr1", Address2 = "addr2", PostCode = "12345" },
new Address { Address1 = "addr1", Address2 = "addr2", PostCode = "12345" },
new Address { Address1 = "addr1", Address2 = "addr2", PostCode = "12345" },
},
},
};
public class Customer
{
public int EmpID { get; set; }
public string Name { get; set; }
public List<Address> Address { get; set; }
public double Salary { get; set; }
}
public class Address
{
public string Address1 { get; set; }
public string Address2 { get; set; }
public string PostCode { get; set; }
}
public class TestClass
{
public void Populate()
{
List<Customer> oCust = new List<Customer>()
{
new Customer() { EmpID=1, Name="Sonia", Address = new List<Address>()
{
new Address { Address1 = "Sonia addr 11", Address2 = "Sonia addr 12", PostCode = "111" },
new Address { Address1 = "Sonia addr 21", Address2 = "Sonia addr 22", PostCode = "222" }
}
},
new Customer() { EmpID=2, Name="Bill", Address = new List<Address>()
{
new Address { Address1 = "Bill addr 11", Address2 = "Bill addr 12", PostCode = "111" },
new Address { Address1 = "Bill addr 21", Address2 = "Bill addr 22", PostCode = "222" }
}
},
new Customer() { EmpID=3, Name="Mark", Address = new List<Address>()
{
new Address { Address1 = "Mark addr 11", Address2 = "Mark addr 12", PostCode = "111" },
new Address { Address1 = "Mark addr 21", Address2 = "Mark addr 22", PostCode = "222" }
}
}
};
}
}
new Customer() { EmpID=1, Name="Sonia",
Address = new List<Address>() {
new Address() { Address1 = "ADDRESS1", PostCode = "000214"} }
},
You have to populate the address the same way you did for Customer like new Customer() { EmpID=3, Name="Mark", new Address(){Address1 = "123 Xyz Avenue", Address2 = "Suite 999", PostalCode = "11111"}}
精彩评论