简体   繁体   中英

Retrieving values from an array using foreach loop

i have added multiple values to an array using 3 different classes that i have created when i use the foreach loop i only get the values from the first class is there any way to use foreach on multiple classes?

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() method:

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

could be an easier way but its the way they want it done in the college.

You can use one foreach block with object as the element type, but you need to check the type of the element, convert the element to the correct type, and implement the logic according to the type of the element.

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

The foreach looping requires that the object implements the System.Collections.IEnumerable or System.Collections.Generic.IEnumerable interface.

So, the answer is "no" There isn't a direct way to use more than ONE object in a foreach looping.

One way to do what you want is with interfaces and a foreach looping, if you make your three classes implement the same Interface. ex:

public interface IInterface
{
    string Text { get; }
}

Then, if you implement this interface in every class, you can do something like this:

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

But you will be able to use only the properties you implement in the interface. SO if you need "different" properties depending on the object, you will have to use some kind of indicator of type and use if's or switchs inside the looping, besides having to implement all the required properties in the interface.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM