使用数组以外的东西来实例化新对象

using something other than a array to instantiate new object

我是一名学生,正在尝试构建一个捕获复杂信息的程序。有一个基础 class 和 2 个 class 继承自基础 class(MultiUnits 和 SingleFamily)。我有这个程序可以使用预加载值,但我也希望能够从用户输入数据。我怎样才能改变数组的大小作为程序 运行s 或者我怎样才能使用列表来存储相同的数据。

我已经尝试将数据存储在列表中并且它有效,但是当我 运行 a for each loop on it 时,我收到以下错误:

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

    }
}

使用 List 代替 SingleFamily[],使用 List

代替 Multi Units[]
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);
        }

    }
}