c sharp project

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Loren Rogers
    New Member
    • Oct 2011
    • 1

    c sharp project

    Doing this project for school.. am not new to computers, but am new to programming.
    I keep getting an error stating that "boardFootPrice " is an unassigned local variable.. Could use some advice.
    here is my code:

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            private int boardLength;
            double totalSales;
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                textBox1.Clear();
                textBox2.Clear();
                textBox3.Clear();
                panel1.Visible = false;
                Oak.Checked = true;
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                const double    OAK_PRICE = 6.00,
                                MAPLE_PRICE = 7.40,
                                WALNUT_PRICE = 8.50;
                int boardLength, boardWidth, boardThickness;
                double boardFeet, boardFootPrice, totalCost;
                if (Oak.Checked)
                    boardFootPrice = OAK_PRICE;
                if (Maple.Checked)
                    boardFootPrice = MAPLE_PRICE;
                if (Walnut.Checked)
                    boardFootPrice = WALNUT_PRICE;
                boardLength = Convert.ToInt32 (textBox1.Text);
                boardWidth = Convert.ToInt32 (textBox2.Text);
                boardThickness = Convert.ToInt32 (textBox3.Text);
                boardFeet = boardLength * (boardWidth / 12.0) * boardThickness;
                totalCost = boardFeet*boardFootPrice;
                totalSales += totalCost;
                label7.Text = string.Format ("{0 2:c6}",boardFeet);
                label8.Text = string.Format ("{0 2:c6}",boardFootPrice);
                label9.Text = string.Format ("{0 2:c6}",totalCost);
                label11.Text = string.Format("{0 2:c6}", totalSales);
                textBox1.Clear();
                textBox2.Clear();
                textBox3.Clear();
                textBox1.Focus();
                Oak.Checked = true;
                button3.Enabled = false;
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                Close();
            }
        }
    }
    Last edited by numberwhun; Oct 7 '11, 03:20 AM. Reason: Please use code tags around the code you put into the forum.
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    In this case the error is correctly describing the problem. You've got a local variable called boardFootPrice that you create without a default value, then you assign a value to it if certain conditions are met. However, there is a possible case none of your conditions are met and boardFootPrice doesn't get a value. You then use it in a calculation farther down.

    The compiler is letting you know that you are using a variable that you haven't assigned a value to. To get rid of this error, you can assign a default value to it when you declare it...

    Code:
    double boardFootPrice = 0.00;
    ... or you can do your assignment in an if-elseif-else block...

    Code:
    double boardFootPrice;
    if (<condition 1>)
      boardFootPrice = 1.10;
    else if (<condition 2>)
      boardFootPrice = 1.25;
    ...
    else
      boardFootPrice = 0.00;
    Whichever you choose is up to you, but make sure that boardFootPrice will always have some value in it :)

    Comment

    Working...