CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2010
    Posts
    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'

  2. #2
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    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;

  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    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.

  4. #4
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    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.
    My hobby projects:
    www.rclsoftware.org.uk

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    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.

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Can't see what's wrong (Short Code Don't Worry)

    Quote Originally Posted by BigEd781 View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured