如下面看到的那样,我有:C#用户定义的数组大小
创建车辆上的类(
Viatura
)。另一个类(
ArrayViatura
),创建车辆和后续方法的数组。
在形式,我有让用户定义该阵列的车辆(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; }
}
}
}`
的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--;
}
}
}`
表单代码
`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'事件中执行此操作 –