使用foreach循环从数组中检索值

时间:2014-12-07 11:38:24

标签: c# arrays loops foreach

我使用我在使用foreach循环时创建的3个不同的类为数组添加了多个值我只从第一个类中获取值是否有任何方法可以在多个类上使用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;
}

可能是一种更简单的方式,但它是他们希望在大学里完成的方式。

2 个答案:

答案 0 :(得分:1)

您可以使用foreachobject作为元素类型,但需要检查元素的类型,将元素转换为正确的类型,并根据元素的类型。

foreach (object obj in array)
{
    if (obj is Member)
    {
        Member m = (Member)obj;
        txtBoxEmail.Text = m.Email;
        txtBoxPhone.Text = m.Phone.ToString();
        txtBoxUsername.Text = m.Username;
    }
    else if (obj is Consoles)
    {
        Consoles c = (Consoles)obj;
        cmbConsole.Text = c.ConsoleName;
    }
    else if (obj is Advert)
    {
        Advert a = (Advert)obj;
        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;
    }
}

答案 1 :(得分:0)

foreach循环要求对象实现System.Collections.IEnumerable或System.Collections.Generic.IEnumerable接口。

所以,答案是“不” 在foreach循环中没有直接使用多个对象的方法。

如果您使三个类实现相同的接口,那么实现所需的一种方法是使用接口和foreach循环。例如:

public interface IInterface
{
    string Text { get; }
}

然后,如果在每个类中实现此接口,则可以执行以下操作:

foreach (IInterface i in array)
{
    //do whatever you want with the text here.
}

但是您将只能使用您在界面中实现的属性。 因此,如果根据对象需要“不同”属性,则必须使用某种类型的指示符,并在循环内使用if或者切换,除了必须在接口中实现所有必需的属性。