1
It is wrong to put this amount of code in an event of click button?
private void CPeBTNSalvar_Click(object sender, EventArgs e)
{
try
{
if (CPeTBCodigo.Text == "")
{
throw new ExcecaoModificada("Determine um codigo para o pedido!");
}
int codigo = int.Parse(CPeTBCodigo.Text);
int ano = CpeData.Value.Year;
int mes = CpeData.Value.Month;
int dia = CpeData.Value.Day;
bool semPedido = true;
Pedido p = new Pedido(codigo, ano, mes, dia);
foreach (DataGridViewRow x in dG_CPeListarProdutos.Rows)
{
if (Convert.ToInt32(x.Cells[4].Value) > 0)
{
ItemPedido item = new ItemPedido(FormularioPrincipal.lProduto[x.Index], Convert.ToInt32(x.Cells[4].Value), Convert.ToDouble(x.Cells[3].Value));
p.itens.Add(item);
semPedido = false;
x.Cells[3].Value = 0;
x.Cells[4].Value = 0;
}
else
{
x.Cells[3].Value = 0;
}
}
if (semPedido == true)
{
throw new ExcecaoModificada("Escolha algum produto!");
}
else
{
CPeLabelAlerta.Visible = true;
CPeLabelAlerta.Text = "Novo pedido adicionado!";
CPeLabelAlerta.ForeColor = Color.DarkSlateGray;
FormularioPrincipal.lPedido.Add(p);
}
}
catch (ExcecaoModificada erro)
{
CPeLabelAlerta.Visible = true;
CPeLabelAlerta.Text = erro.Message;
CPeLabelAlerta.ForeColor = Color.Red;
}
catch (Exception)
{
CPeLabelAlerta.Visible = true;
CPeLabelAlerta.Text = "Coloque apenas numeros em código!";
}
}
If you think it’s not wrong, quote something you wouldn’t do like I did.
Normally I would create a function and only call in the button event, besides facilitating the call at other events would make the code more visually organized :)
– Tuxpilgrim
I think there is no need to keep throwing exceptions like that. Take a look here. Surely you are using the feature wrongly.
– gato
If a function has too much code, it can probably be divided into smaller functions, for example, a separate function that validates a given
– Costamilam
Thanks guys! @cat I will study more of the subject vlw :-D
– Márcio Sebastião