1

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.

2
  • Instead of using three classes, why don't you use just one class that contains all properties of those three classes? Commented Dec 7, 2014 at 11:50
  • the lecturer wants different classes for each table in the database, i had asked could i use one but was told they needed to be separate. I know it would make it alot easier with one Commented Dec 7, 2014 at 11:55

2 Answers 2

1

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;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

ArrayList does implement IEnumerable, see here: msdn.microsoft.com/en-us/library/…
It implements, but if you have MORE than one type, you need to use a common interface to use it as a single thing. But I agree that the "if" option on your answer is probably best for the case in question :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.