如何将内置数据类型数组更改为自定义数据类型数组

时间:2018-11-27 07:53:59

标签: c#

我想创建一个自定义数据类型数组,但事实证明,我不是偶然地创建了一个内置数据类型数组。如何更改下面的代码以创建自定义数据类型数组?

using System;
using System.Collections;
using System.Windows.Forms;

namespace Question6
{
    public partial class Question6 : Form
    {
        //create an ArrayList
        ArrayList vehicleList = new ArrayList();

        public Question6()
        {
            InitializeComponent();

        }

        private void btnCreateVehicle_Click(object sender, EventArgs e)
        {
            //variables that store the user input data
            String registration;
            String brand;
            String model;
            Int32 wheels;
            Int32 doors;
            String bodyType;
            Decimal weight;
            //user input validation
            foreach (Control c in this.Controls)
            {
                if (c is TextBox)
                {
                    if (String.IsNullOrWhiteSpace(c.Text) == true)
                    {
                        MessageBox.Show("Please fill all the blanks.");
                        break;
                    }
                    else {
                        //assign user input to the variables
                        registration = txbReg.Text;
                        brand = txbBrand.Text;
                        model = txbModel.Text;
                        bodyType = txbBodyType.Text;

                        if (!Int32.TryParse(txbWheels.Text, out wheels))
                        {
                            MessageBox.Show("Wheels should be an integer.");
                        }

                        if (!Int32.TryParse(txbDoors.Text, out doors))
                        {
                            MessageBox.Show("Doors should be an integer");
                        }

                        if (!Decimal.TryParse(txbDoors.Text, out weight))
                        {
                            MessageBox.Show("Weight should be a decimal");
                        }
                        //create an instance of Vehicles and add to the ArrayList
                        Vehicles vehicles = new Vehicles(registration, brand, model, wheels, doors, bodyType, weight);
                        vehicleList.Add(vehicles);

                        display();
                        break;
                    }
                }
            }

        }

        //display each object in the ArrayList
        void display()
        {
            lsvVehicles.Items.Clear();
            foreach (Vehicles v in vehicleList)
            {
                ListViewItem item = new ListViewItem();
                item.Text = v.Registration;
                item.SubItems.Add(v.Brand);
                item.SubItems.Add(v.Model);
                item.SubItems.Add(v.Wheels.ToString());
                item.SubItems.Add(v.Doors.ToString());
                item.SubItems.Add(v.BodyType);
                item.SubItems.Add(v.Weight.ToString());
                lsvVehicles.Items.Add(item);

            }

        }
        //clear the listview
        private void btnClean_Click(object sender, EventArgs e)
        {
            lsvVehicles.Items.Clear();
        }
    }

    public class Vehicles
    {
        //class member
        String registration;
        String brand;
        String model;
        Int32 wheels;
        Int32 doors;
        String bodyType;
        Decimal weight;

        //constructor
        public Vehicles(string registration, string brand, string model, int wheels, int doors, string bodyType, decimal weight)
        {
            this.Registration = registration;
            this.Brand = brand;
            this.Model = model;
            this.Wheels = wheels;
            this.Doors = doors;
            this.BodyType = bodyType;
            this.Weight = weight;
        }

        //property
        public string Registration { get => registration; set => registration = value; }
        public string Brand { get => brand; set => brand = value; }
        public string Model { get => model; set => model = value; }
        public int Wheels { get => wheels; set => wheels = value; }
        public int Doors { get => doors; set => doors = value; }
        public string BodyType { get => bodyType; set => bodyType = value; }
        public decimal Weight { get => weight; set => weight = value; }
    }
}

0 个答案:

没有答案