0

I'm a student trying to build a program that captures information of a complex. There is a base class and 2 classes inheriting from the base class(MultiUnits and SingleFamily). I've got this program that works with values pre load but I want to be able to also enter data from the user. how can I change the size of the array as the program runs or how can I use a list to store the same data.

I have tried storing the data in a list and it works but when i run a for each loop on it i get the following error:

CS1579 foreach statement cannot operate on variables of type 'SingleFamily' because 'SingleFamily' does not contain a public instance or extension definition for 'GetEnumerator'

public partial class HousingPresentationGUI : Form
{
    SingleFamily[] privateHome;
    MultiUnits[] multi;
    public HousingPresentationGUI()
    {
        InitializeComponent();
    }

    private void HousingPresentationGUI_Load(object sender, EventArgs e)
    {
        privateHome = new SingleFamily[5];

        privateHome[0] = new SingleFamily("34 Winston Street", 3, 2, 900.00m);
        privateHome[1] = new SingleFamily("5234 Carolina Ave", 2, 2, 850.00m);
        privateHome[2] = new SingleFamily("54 Magnolia Court", 4, 2, 1150.00m);
        privateHome[3] = new SingleFamily("6910 Reiley", 3, 2, 1000.00m);
        privateHome[4] = new SingleFamily("76 St. Johns Ct.", 3, 2, 1000.00m);

        foreach (SingleFamily homes in privateHome)
        {
            lstBxSingle.Items.Add(homes.Address);
        }


        multi = new MultiUnits[2];

        multi[0] = new MultiUnits("8674 Victoria Lane", 2, 750.00m);
        multi[1] = new MultiUnits("9724 Bridge Street", 2, 700.00m);


        foreach (MultiUnits duplex in multi)
        {
            lstBxMulti.Items.Add(duplex.Address);
        }

    }

    private void cmboTypeOfRental_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblRentalDetails.Text = "";

        lblInstructions.Visible = true;
        if (cmboTypeOfRental.SelectedIndex == 0)
        {
            lstBxSingle.Visible = true;
            lstBxMulti.Visible = false;
        }
        else
        {
            lstBxSingle.Visible = false;
            lstBxMulti.Visible = true;
        }

    }

    private void lstBxMulti_SelectedIndexChanged(object sender, EventArgs e)
    {
        foreach (MultiUnits duplex in multi)
        {
            if (duplex.Address == lstBxMulti.SelectedItem.ToString())
                lblRentalDetails.Text = duplex.ToString();
        }
    }

    private void lstBxSingle_SelectedIndexChanged(object sender, EventArgs e)
    {

        foreach (SingleFamily home in privateHome)
        {
            if (home.Address == lstBxSingle.SelectedItem.ToString())
                lblRentalDetails.Text = home.ToString();
        }

    }
}
2
  • your problem isn't arrays or lists its that your treating SingleFamily as if it implements IEnumerable and its doesn't Commented Mar 31, 2022 at 13:30
  • 1
    Instead of SingleFamily[] use List<SingleFamily> and isntead of MultiUnits[] use List<MultiUnits> Commented Mar 31, 2022 at 13:32

1 Answer 1

1

Instead of SingleFamily[] use List and isntead of MultiUnits[] use List

public partial class HousingPresentationGUI : Form
{
    List<SingleFamily> privateHome = new List<SingleFamily>();
    List<MultiUnits> multi= new List<MultiUnits>();

    public HousingPresentationGUI()
    {
        InitializeComponent();
    }

    private void HousingPresentationGUI_Load(object sender, EventArgs e)
    {
        privateHome.Add(new SingleFamily("34 Winston Street", 3, 2, 900.00m));
        ....etc etc add the other private homes

        foreach (SingleFamily homes in privateHome)
        {
            lstBxSingle.Items.Add(homes.Address);
        }

        multi.Add(new MultiUnits("8674 Victoria Lane", 2, 750.00m));
        multi.Add(new MultiUnits("9724 Bridge Street", 2, 700.00m));

        foreach (MultiUnits duplex in multi)
        {
            lstBxMulti.Items.Add(duplex.Address);
        }

    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

error in your example, you have changed multi to a list but left the array functionality unchanged
@MikeT I know, i changed the first part to show him, the second part with multi he can edit on his own.
@MikeT changed it now
@Wet_pantz thank you so so much been stuck on this for 5 hours, thank you

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.