【问题标题】:C# User defined Array SizeC# 用户定义的数组大小
【发布时间】:2018-02-09 11:35:52
【问题描述】:

如下所示,我有:

  1. 创建车辆的类 (Viatura)。

  2. 另一个类 (ArrayViatura) 创建一个 Vehicles 数组和后续方法。

在表单中,我必须让用户在执行表单中的任何其他操作之前定义此车辆数组 (numericupdown1) 的大小

如何让这个值变成数组大小?

提前致谢!

这是代码:

类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; }

        }
    }
}`

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--;
        }
    }
}`

表单代码

`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;
        }
    }
}`

【问题讨论】:

  • 将其设为变量(例如此类中的字段)并在询问用户后分配值
  • @TimSchmelter 我将如何以及在表单结构中的何处执行此操作?
  • @GonçaloDias 你可以在 NumericUpDown 的 ValueChanged 事件中做到这一点

标签: c# arrays forms size


【解决方案1】:

假设大小在TextBox1中定义:

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

但请注意,直接从用户那里获取数组大小不是一个好主意,但您可以在确定用户需求后自己定义数组大小。

如果大小在 NumericUpDown 中定义,则:

public ArrayColab colaborators = new ArrayColab(NumericUpDown1.Value);

【讨论】:

  • 大小在 numericupdown 中定义。我不能使用int.TryParse,因为我不在一个方法中,而是在上面public partial class Winnings: Form {
  • @GonçaloDias 我编辑了我的答案。请看一看。
  • 好吧我明白了,但有一件事,它不允许我使用 NumericUpDown1.Value 作为数组的大小定义,因为 字段初始化器不能引用非静态字段、方法或属性
  • @GonçaloDias ArrayColab 的类型是什么,你能发布它的结构吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-22
  • 2016-02-10
  • 1970-01-01
  • 2019-08-10
  • 2013-01-19
  • 2019-11-26
  • 2015-08-26
相关资源
最近更新 更多