我已經添加了多個值到一個數組使用3個不同的類,我創建時,我使用foreach循環我只從第一類獲取值是否有任何方式使用foreach多個班級?使用foreach循環從數組中檢索值
AdvertDao advert = new AdvertDao();
var array = new ArrayList();
array = advert.fillAdvert();
foreach (Member m in array)
{
txtBoxEmail.Text = m.Email;
txtBoxPhone.Text = m.Phone.ToString();
txtBoxUsername.Text = m.Username;
}
foreach (Consoles c in array)
{
cmbConsole.Text = c.ConsoleName;
}
foreach (Advert a in array)
{
cmbGenre.Text = a.Genre;
lblDateStarted.Text = a.Date.ToString("dd/MM/yyyy");
txtBoxPrice.Text = a.Price.ToString();
txtBoxName.Text = a.Name;
txtBoxDesc.Text = a.Description;
}
fillAdvert()
方法:
public ArrayList fillAdvert()
{
Member member = new Member();
Advert advert = new Advert();
Consoles console = new Consoles();
Picture picture = new Picture();
ArrayList advertList = new ArrayList();
if (!DatabaseConnection.IsOpen)
{
DatabaseConnection.Open();
}
OracleCommand cmd = new OracleCommand();
cmd.Connection = DatabaseConnection.Connection;
string str = "SELECT * FROM ADVERT_ADPIC_MEMBER_CONSOLE WHERE username = '" + GlobalVariables.Username + "' AND name = '" + GlobalVariables.SellingName + "'";
cmd.CommandText = str;
OracleDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
member.Username = dr.GetString(0);
member.MemberID = dr.GetInt32(1);
member.Phone = dr.GetInt32(2);
member.Email = dr.GetString(3);
console.ConsoleName = dr.GetString(5);
advert.Description = dr.GetString(6);
advert.Genre = dr.GetString(7);
advert.Date = dr.GetDateTime(8);
advert.Price = dr.GetDouble(9);
advert.Name = dr.GetString(4);
advertList.Add(member);
advertList.Add(console);
advertList.Add(advert);
}
return advertList;
}
可能是一個更簡單的方法,但他們希望在大學完成了它的方式。
而不是使用三個類,爲什麼不使用一個包含這三個類的所有屬性的類? – ekad 2014-12-07 11:50:09
講師希望數據庫中的每個表有不同的類,我曾問過我可以使用一個,但被告知他們需要分開。我知道這會讓它更容易一個 – user3433399 2014-12-07 11:55:34