-
September 13th, 2010, 10:20 AM
#1
Can't see what's wrong (Short Code Don't Worry)
Hi, i would love if you could see what's wrong in this code...
private void btnCalculer_Click(object sender, EventArgs e)
{
double Quant, Cout, PrixU, QuantV, Prix, Profit;
try
{
Quant = Convert.ToDouble(txtQuant.Text);
Cout = Convert.ToDouble(txtCout.Text);
QuantV = Convert.ToDouble(txtQuantV.Text);
Prix = Convert.ToDouble(txtPrix.Text);
}
catch
{
MessageBox.Show("Erreur, remplir toutes les cases et utilisé des , et non des .", "Erreur");
}
ERROR PrixU = Cout / Quant;
ERROR Profit = (Prix - (QuantV * PrixU));
try
{
txtPrixU.Text = Convert.ToString(PrixU);
txtProfit.Text = Convert.ToString(Profit);
}
catch
{
MessageBox.Show("FATAL ERROR!", "FATAL ERROR");
}
I got an error for each variable in catch fonction
Use of unassigned local variable 'Cout'
Use of unassigned local variable 'Quant'
Use of unassigned local variable 'Prix'
Use of unassigned local variable 'QuantV'
-
September 13th, 2010, 11:37 AM
#2
Re: Can't see what's wrong (Short Code Don't Worry)
change
Code:
double Quant, Cout, PrixU, QuantV, Prix, Profit;
into
Code:
double Quant = 0, Cout = 0, PrixU = 0, QuantV = 0, Prix = 0, Profit = 0;
-
September 13th, 2010, 02:32 PM
#3
Re: Can't see what's wrong (Short Code Don't Worry)
It helps to read the error message and then look it up if you don't understand it. It is not the last error you are going to have to deal with, and this one is pretty straightforward.
-
September 14th, 2010, 08:09 AM
#4
Re: Can't see what's wrong (Short Code Don't Worry)
You don't normally need to initialise the local variables. The compiler can see whether they will be initialised before being used. In this case, however, the catch statement does make it possible that they will be used without being initialised.
Also, even if Quant is initialised to zero there can still be a 'divide-by-zero' exception!
Last edited by Zaccheus; September 14th, 2010 at 08:12 AM.
-
September 14th, 2010, 05:53 PM
#5
Re: Can't see what's wrong (Short Code Don't Worry)
There are really no good arguments against initializing variables, and there are plenty of arguments for it. In C# it is less error prone because you can typically count on, for example, an integer being initialized to 0, but still, just initialize it anyway.
-
September 14th, 2010, 08:55 PM
#6
Re: Can't see what's wrong (Short Code Don't Worry)
Originally Posted by BigEd781
There are really no good arguments against initializing variables, and there are plenty of arguments for it. In C# it is less error prone because you can typically count on, for example, an integer being initialized to 0, but still, just initialize it anyway.
About the only reason for not doing it is tools like Resharper tend to balk when you do. The flag it as unnecessary (which in .net, it is).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|