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();
}
}
}
CodePudding user response:
Either write an array handleing function, that lengthens or shortens the array automatically by copying and recreating it, or - easier - use a List
List<SingleFamily> privateHome = new List<SingleFamily>()
privateHome.add(...)
The list then should have and implementation if IEnumerable, arrays do not have this.
CodePudding user response:
{
var privateHome = new List<SingleFamily>();
var multi= new List<MultiUnits>();
public HousingPresentationGUI()
{
InitializeComponent();
}
private void HousingPresentationGUI_Load(object sender, EventArgs e)
{
privateHome = new SingleFamily[5];
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 = 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();
}
}
}
}