2011-08-31 107 views
0

這是我的階級結構需要幫助的C#編碼

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"}, 
     }; 
    } 

從populate方法,我與地址一起填充客戶。 當我填充然後客戶如何與它一起 填入地址我不是一個高級用戶,請指導我有關的代碼,如何 new Customer() { EmpID=3, Name="Mark" new Address{}}

請幫助....感謝

+0

你可能要考慮'Address'取出二傳手,因爲你可以添加到列表中,而無需更改整個列表... –

+0

您的代碼很混亂。客戶是否應該有一個地址或多個地址? – Achim

+3

這是你能想出的最好的頭銜嗎? –

回答

1
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" } 
             } 
            } 
      }; 
    } 
} 
11
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" }, 
     } 
    } 
}; 
+2

你不需要'新列表

',因爲它是在這裏的構造函數創建的... –

+0

@Reed Copsey,完美,謝謝你指出這一點。我已經更新了我的答案並將其考慮在內。它使代碼更短。 –

3

使用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" }, 
     }, 
    }, 
}; 
0

你必須填寫的地址你沒有像新客戶的客戶(){ 的EmpID = 3的相同方式,名稱=「標記」,新廣告禮服(){地址1 ​​= 「123的Xyz大道」,地址2 = 「999套房」,POSTALCODE = 「11111」}}

1
new Customer() { EmpID=1, Name="Sonia", 
         Address = new List<Address>() { 
          new Address() { Address1 = "ADDRESS1", PostCode = "000214"} } 
        },