0

As seen below, I have:

  1. A class (Viatura) that creates a Vehicle.

  2. Another class (ArrayViatura) that creates an array of Vehicles and subsequent methods.

In the form, I have to let the user define the size of this array of vehicles (numericupdown1), before doing any other operations within the form.

How do I make this value become the array size?

Thanks in Advance!

Here's the Code:

Class Viatura

`namespace IP_GonçaloDias_G00
{
    class Viatura
    {
        string cvMatrícula;
        string cvMarca;
        string cvModelo;
        string cvAnoFabrico;
        string cvTipoPropulsão;
        string cvCilindrada;
        string cvPotência;
        double cvAceleração;
        string cvConsumoMédio;
        string cvCor;
        int cvTipoVeículo;
        string cvCaixa;
        DateTime cvPrimeiraMatrícula;
        int cvNúmeroRegistos;
        double cvKMPercorridos;
        string cvDescriçãoVeículo;
        double cvPreçoAquisição;
        double cvPreçoProposto;
        double cvPreçoVenda;
        DateTime cvDataVenda;
        string cvNomeCliente;

        public Viatura(string matricula, string marca, string modelo, string anofabrico, string tipopropulsao, string cilindrada, string potencia, double aceleracao, string consumomedio, string cor, int tipoveiculo, string caixa, DateTime primeiramatricula, int numeroregistos, double km, string descricaoveiculo, double precoaquisicao, double precoproposto, double precovenda, DateTime datavenda, string nomecliente)
        {
            string cvMatrícula=matricula;
            string cvMarca=marca;
            string cvModelo=modelo;
            string cvAnoFabrico=anofabrico;
            string cvTipoPropulsão=tipopropulsao;
            string cvCilindrada=cilindrada;
            string cvPotência=potencia;
            double cvAceleração=aceleracao;
            string cvConsumoMédio=consumomedio;
            string cvCor=cor;
            int cvTipoVeículo=tipoveiculo;
            string cvCaixa=caixa;
            DateTime cvPrimeiraMatrícula=primeiramatricula;
            int cvNúmeroRegistos=numeroregistos;
            double cvKMPercorridos=km;
            string cvDescriçãoVeículo=descricaoveiculo;
            double cvPreçoAquisição=precoaquisicao;
            double cvPreçoProposto=precoproposto;
            double cvPreçoVenda=precovenda;
            DateTime cvDataVenda=datavenda;
            string cvNomeCliente =nomecliente;
        }

        public string CVMatrícula
        {
            get { return cvMatrícula; }
            set { cvMatrícula = value; }
        }
        public string CVMarca
        {
            get { return cvMarca; }
            set { cvMarca = value; }
        }
        public string CVModelo
        {
            get { return cvModelo; }
            set { cvModelo = value; }
        }
        public string CVAnoFabrico
        {
            get { return cvAnoFabrico; }
            set { cvAnoFabrico = value; }
        }
        public string CVTipoPropulsão
        {
            get { return cvTipoPropulsão; }
            set { cvTipoPropulsão = value; }

        }
        public string CVCilindrada
        {
            get { return cvCilindrada; }
            set { cvCilindrada = value; }

        }
        public string CVPotência
        {
            get { return cvPotência; }
            set { cvPotência = value; }

        }
        public double CvAceleração
        {
            get { return cvAceleração; }
            set { cvAceleração = value; }

        }
        public string CVConsumoMédio
        {
            get { return cvConsumoMédio; }
            set { cvConsumoMédio = value; }

        }
        public string CVCor
        {
            get { return cvCor; }
            set { cvCor = value; }

        }
        public int CVTipoVeículo
        {
            get { return cvTipoVeículo; }
            set { cvTipoVeículo = value; }

        }
        public string CVCaixa
        {
            get { return cvCaixa; }
            set { cvCaixa = value; }

        }
        public DateTime CVPrimeiraMatrícula
        {
            get { return cvPrimeiraMatrícula; }
            set { cvPrimeiraMatrícula = value; }

        }
        public int CVNúmeroRegistos
        {
            get { return cvNúmeroRegistos; }
            set { cvNúmeroRegistos = value; }

        }
        public double CVKMPercorridos
        {
            get { return cvKMPercorridos; }
            set { cvKMPercorridos = value; }

        }
        public string CVDescriçãoVeículo
        {
            get { return cvDescriçãoVeículo; }
            set { cvDescriçãoVeículo = value; }

        }
        public double CVPreçoAquisição
        {
            get { return cvPreçoAquisição; }
            set { cvPreçoAquisição = value; }

        }
        public double CVPreçoProposto
        {
            get { return cvPreçoProposto; }
            set { cvPreçoProposto = value; }

        }
        public double CVPreçoVenda
        {
            get { return cvPreçoVenda; }
            set { cvPreçoVenda = value; }

        }
        public DateTime CVDataVenda
        {
            get { return cvDataVenda; }
            set { cvDataVenda = value; }

        }
        public string CVNomeCliente
        {
            get { return cvNomeCliente; }
            set { cvNomeCliente = value; }

        }
    }
}`

The Class ArrayViatura

`namespace IP_GonçaloDias_G00
{
    class ArrayViaturas
    {
        public Viatura[] viaturas;
        private int numElementos;
        private int pointer;

        public ArrayViaturas(int nElem)
        {
            viaturas = new Viatura[nElem];
            numElementos = 0;
            pointer = 0;
        }
        public int NumElementos
        {
            set { numElementos = value; }
            get { return numElementos; }
        }
        public int Pointer
        {
            set { pointer = value; }
            get { return pointer; }
        }
        public void InserirViatura(string matricula, string marca, string modelo, string anofabrico, string tipopropulsao, string cilindrada, string potencia, double aceleracao, string consumomedio, string cor, int tipoveiculo, string caixa, DateTime primeiramatricula, int numeroregistos, double km, string descricaoveiculo, double precoaquisicao, double precoproposto, double precovenda, DateTime datavenda, string nomecliente)
        {
            viaturas[numElementos] = new Viatura(matricula, marca, modelo, anofabrico, tipopropulsao, cilindrada, potencia, aceleracao, consumomedio, cor, tipoveiculo, caixa, primeiramatricula, numeroregistos, km, descricaoveiculo, precoaquisicao, precoproposto, precovenda, datavenda, nomecliente);
            numElementos++;
        }
        public string MostrarViatura(int index, string sep)
        {
            string str = viaturas[index].CVMatrícula + sep + viaturas[index].CVMarca + sep + viaturas[index].CVModelo + sep + viaturas[index].CVAnoFabrico +
                sep + viaturas[index].CVTipoPropulsão + sep + viaturas[index].CVCilindrada + sep + viaturas[index].CVPotência +
                sep + viaturas[index].CvAceleração.ToString("f2") + "KMh" + sep + viaturas[index].CVConsumoMédio + sep + viaturas[index].CVCor
                + sep + viaturas[index].CVTipoVeículo.ToString("f2") + sep + viaturas[index].CVCaixa + sep + viaturas[index].CVPrimeiraMatrícula.ToShortDateString()
                + sep + viaturas[index].CVNúmeroRegistos.ToString("f2") + sep + viaturas[index].CVKMPercorridos.ToString("f2") + sep + viaturas[index].CVDescriçãoVeículo +
                sep + viaturas[index].CVPreçoAquisição.ToString("f2") + sep + viaturas[index].CVPreçoProposto.ToString("f2") + sep + viaturas[index].CVPreçoVenda.ToString("f2") +
                sep + viaturas[index].CVNomeCliente;
            return str;
        }
        public void EliminarViatura(int index)
        {
            for (int i = index; i < NumElementos - 1; i++)
            {
                viaturas[i] = viaturas[i + 1];
            }
            NumElementos--;
            if (pointer == NumElementos)
                pointer--;
        }
    }
}`

The Form Code

`namespace IP_GonçaloDias_G00
{
    public partial class RegistoViaturas : Form
    {

        string cvMatrícula="";
        string cvMarca = "";
        string cvModelo = "";
        string cvAnoFabrico = "";
        string cvTipoPropulsão = "";
        string cvCilindrada = "";
        string cvPotência = "";
        double cvAceleração = 0;
        string cvConsumoMédio = "";
        string cvCor = "";
        int cvTipoVeículo = 0;
        string cvCaixa = "";
        DateTime cvPrimeiraMatrícula=DateTime.Now;
        int cvNúmeroRegistos = 0;
        double cvKMPercorridos = 0;
        string cvDescriçãoVeículo = "";
        double cvPreçoAquisição = 0;
        double cvPreçoProposto = 0;
        double cvPreçoVenda = 0;
        DateTime cvDataVenda = DateTime.Now;
        string cvNomeCliente = "";
        public RegistoViaturas()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            int size= Convert.ToInt32(numericUpDown1.Value);
            ArrayViaturas viaturas = new ArrayViaturas(size);

            MessageBox.Show("O tamanho definido para o Array é:   " + viaturas.viaturas.Length);
            groupBox2.Enabled = true;
        }
    }
}`
3
  • Make it a variable(for example a field in this class) and assign the value after you have asked the user Commented Aug 31, 2017 at 13:31
  • @TimSchmelter How and where within the form structure would I do this? Commented Aug 31, 2017 at 13:54
  • @GonçaloDias You can do it in the ValueChanged event of the NumericUpDown Commented Aug 31, 2017 at 13:56

1 Answer 1

1

Assuming that the size is defined in TextBox1:

int size = 20;
int.TryParse(TextBox1.Text, out size);
public ArrayColab colaborators = new ArrayColab(size);

But note that its not a good idea to get the array size directly from user but you can define the array size yourself after detemening the user's need.

If the size is defined in NumericUpDown then:

public ArrayColab colaborators = new ArrayColab(NumericUpDown1.Value);
Sign up to request clarification or add additional context in comments.

4 Comments

The size is defined in a numericupdown. I cannot use the int.TryParse as I am not within a method but up there public partial class Winnings: Form {
@GonçaloDias I edited my answer. please have a look.
Okay I understand but one thing, it doesn't let me use NumericUpDown1.Value as the size definition of the array, because the field initializer cannot reference the non-static field, method or property
@GonçaloDias what is the type of ArrayColab, could you please post its structure?

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.