如何将包含arraylist的对象添加到另一个arraylist,C#?

时间:2017-12-13 17:24:57

标签: c# oop object arraylist streamwriter

我在C#中制作一个Windows应用程序,它从文本文件中读取有关汽车租赁公司的数据,并将这些信息输出到两个列表框(公司和汽车)中。有2个类CompanyCars,它们读入数组:

 public void readFile()
    {
        string IDComp, compName, compAddress, compPost;
        int compNumCars;
        string IDcars, carsMM, carsReg, carsFuel, carsServ, carsComments;
        ArrayList carArray;


        //start streamreader (open file)
        StreamReader readFile = new StreamReader(theFile);

        //read the file and bring in the data
        while (!readFile.EndOfStream)
        {
            //get raw company info  
            IDComp = readFile.ReadLine();
            compName = readFile.ReadLine();
            compAddress = readFile.ReadLine();
            compPost = readFile.ReadLine();
            compNumCars = Convert.ToInt32(readFile.ReadLine());

            //get car array read in for company 
            carArray = new ArrayList(); 
            for (int i = 0; i < compNumCars; i++)
            {
                //get car data
                IDcars = readFile.ReadLine();
                carsMM = readFile.ReadLine();
                carsReg = readFile.ReadLine();
                carsFuel = readFile.ReadLine();
                carsServ = readFile.ReadLine();
                carsComments = readFile.ReadLine();

                //create a car object
                Cars newCars = new Cars(IDcars, carsMM, carsReg, carsFuel, carsServ, carsComments);

                //add it to array
                carArray.Add(newCars);

            }   //car array done!


            //create a Company object
            Company newComp = new Company(IDComp, compName, compAddress, compPost, compNumCars, carArray);

            //add company object to array
            companyArray.Add(newComp);
        }
        readFile.Close();

    }

我有一个工作的编写器,我可以成功编辑现有汽车和公司的信息:(更新列表框的代码未显示)

                                                             //SAVE EDIT

    private void btnsaveeditcomp_Click(object sender, EventArgs e)
    {

        Company editcomp = (Company)companyArray[lstCompany.SelectedIndex];

        editcomp.setCompID(txtEditcompID.Text);
        editcomp.setName(txtEditName.Text);
        editcomp.setAddress(txtEditAddress.Text);
        editcomp.setPostcode(txtEditPost.Text);
        editcomp.setNumCars(Convert.ToInt32(txtEditNumCars.Text));

        updateSelectdComp();
        btnSaveEditComp.Hide();
        writeFile();


    }


    private void saveeditcars_Click(object sender, EventArgs e)
    {
        // Cars editCars = (Cars)carsArray[lstCars.SelectedIndex];
        ArrayList tmpCars = ((Company)companyArray[selectedComp]).getFleet();

        Cars newcars = (Cars)tmpCars[selectedCar];

        newcars.setCarsID(txtEditcarID.Text);
        newcars.setMakeModel(txtEditMM.Text);
        newcars.setReg(txtEditReg.Text);
        newcars.setFuel(txtEditFuel.Text);
        newcars.setServdate(txtEditServdate.Text);
        newcars.setComments(txtEditComments.Text);

        updateSelectedCar(); 
        btnSaveEditCars.Hide();
        writeFile();

    } 

FleetCompany课程中的一个arraylist,它存储有关每家公司特定汽车的信息。

如何添加新的公司对象?新公司最初不必安装任何汽车,我将使用与(txtEditcompID.Text, ect.)相同的文本框输入我想要创建的公司的信息。

感谢您的帮助,非常感谢:)

0 个答案:

没有答案