Visual C# Developer Center > Visual C# Forums > Visual C# Language > C# variable scope program issue
Ask a questionAsk a question
 

AnswerC# variable scope program issue

  • Thursday, November 05, 2009 8:49 PMDivisionBy0 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hey guys. I'm having trouble with a program that I'm attempting to write. I've got most of the way through, and I can run the program no problem. The issue stems from the output. I'm able to get the current charge and number of groups output to work correctly, but I'm having trouble getting the accumulated charge and the average charge.
    Here is my code:

    namespace Recording_Studio
    {
        public partial class Form1 : Form
        {
            //Global Declarations: Accumulated Charges, Total Amount of Groups, and a constant for the Rental Rate
            double totalAccumulatedCharges = 0;
            double totalNumberOfGroups = 0;
            double rentalRate = 200 / 60;
            public Form1()
            {
                InitializeComponent();
            }

            private void Form1_Load(object sender, EventArgs e)
            {

            }

            private void btnCalculate_Click(object sender, EventArgs e)
            {
                // Declarations: Input - Name, Minutes used.
                //               Output - Current charge, Group name, Accumulated Charges, Average Charge, Number of Groups
                double dblMinutesUsed;
                double dblAverageCharge, dblCurrentCharge, dblAccumulatedCharges, dblNumberOfGroups;
                dblAccumulatedCharges = 0;
                dblNumberOfGroups = 1;

                //Input - Gather the data
                try
                {
                    dblMinutesUsed = double.Parse(txtMinutesUsed.Text);

                }
                catch
                {
                    MessageBox.Show("Input was either non-numeric or blank", "Invalid Input!");
                    txtMinutesUsed.Focus();
                    return;
                }

                //Process - Calculate Current Charge, Accumulated Charges, Number of Groups, and Average charge
                dblCurrentCharge = dblMinutesUsed * rentalRate;
                dblAverageCharge = totalAccumulatedCharges / dblNumberOfGroups;
                dblAccumulatedCharges = dblCurrentCharge + totalAccumulatedCharges;
                dblNumberOfGroups += totalNumberOfGroups;
                totalNumberOfGroups++;
              
                //Output - Display the information

                //Clear all the output listboxes
                lstAccumulatedCharges.Items.Clear();
                lstAverageCharge.Items.Clear();
                lstCurrentCharge.Items.Clear();
                lstGroupName.Items.Clear();
                lstNumberOfGroups.Items.Clear();

                //Display the information
                lstAccumulatedCharges.Items.Add(dblAccumulatedCharges.ToString("n1"));
                lstAverageCharge.Items.Add(dblAverageCharge.ToString("n1"));
                lstCurrentCharge.Items.Add(dblCurrentCharge.ToString("n1"));
                lstGroupName.Items.Add(txtGroupName.Text);
                lstNumberOfGroups.Items.Add(dblNumberOfGroups.ToString("n1"));

            }

            private void btnClear_Click(object sender, EventArgs e)
            {
                // Clear all input text boxes and output list boxes
                txtGroupName.Clear();
                txtMinutesUsed.Clear();
                lstAccumulatedCharges.Items.Clear();
                lstAverageCharge.Items.Clear();
                lstCurrentCharge.Items.Clear();
                lstGroupName.Items.Clear();
                lstNumberOfGroups.Items.Clear();
                txtGroupName.Focus();
            }

            private void btnExit_Click(object sender, EventArgs e)
            {
                this.Close();
            }
        }
    }

    I'm figuring that it has something to do with the scope of a declaration?
    But I'm not absolutely sure, and I have no idea how to fix this. I've tried a bunch of different things. Any help would be greatly appreciated!
    Thank you!

Answers

  • Thursday, November 05, 2009 11:27 PMK.MAK Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    I'm not sure about ur function and what do you try to do? Your btnCalculate_click event, I have some notes:

            private void btnCalculate_Click(object sender, EventArgs e)
            {

    • //Do you want to reset the value every time you click on the button?
                // Declarations: Input - Name, Minutes used.
                //               Output - Current charge, Group name, Accumulated Charges, Average Charge, Number of Groups
                double dblMinutesUsed;
                double dblAverageCharge, dblCurrentCharge, dblAccumulatedCharges, dblNumberOfGroups;
                dblAccumulatedCharges = 0;
                dblNumberOfGroups = 1;

                //Input - Gather the data
                try
                {
                    dblMinutesUsed = double.Parse(txtMinutesUsed.Text);

                }
                catch
                {
                    MessageBox.Show("Input was either non-numeric or blank", "Invalid Input!");
                    txtMinutesUsed.Focus();
                    return;
                }

    • //Where the value that you get from?

                //Process - Calculate Current Charge, Accumulated Charges, Number of Groups, and Average charge
                dblCurrentCharge = dblMinutesUsed * rentalRate;
                dblAverageCharge = totalAccumulatedCharges / dblNumberOfGroups;
                dblAccumulatedCharges = dblCurrentCharge + totalAccumulatedCharges;
                dblNumberOfGroups += totalNumberOfGroups;
                totalNumberOfGroups++;
              
                //Output - Display the information

                //Clear all the output listboxes
                lstAccumulatedCharges.Items.Clear();
                lstAverageCharge.Items.Clear();
                lstCurrentCharge.Items.Clear();
                lstGroupName.Items.Clear();
                lstNumberOfGroups.Items.Clear();

                //Display the information
                lstAccumulatedCharges.Items.Add(dblAccumulatedCharges.ToString("n1"));
                lstAverageCharge.Items.Add(dblAverageCharge.ToString("n1"));
                lstCurrentCharge.Items.Add(dblCurrentCharge.ToString("n1"));
                lstGroupName.Items.Add(txtGroupName.Text);
                lstNumberOfGroups.Items.Add(dblNumberOfGroups.ToString("n1"));

            }

    If the post is helpful or answers your question, please mark it as such!
    • Marked As Answer byDivisionBy0 Friday, November 06, 2009 1:28 PM
    •  
  • Friday, November 06, 2009 11:16 AMLouis.fr Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    You never change the value of totalAccumulatedCharges
    • Marked As Answer byDivisionBy0 Friday, November 06, 2009 1:28 PM
    •  

All Replies

  • Thursday, November 05, 2009 11:27 PMK.MAK Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    I'm not sure about ur function and what do you try to do? Your btnCalculate_click event, I have some notes:

            private void btnCalculate_Click(object sender, EventArgs e)
            {

    • //Do you want to reset the value every time you click on the button?
                // Declarations: Input - Name, Minutes used.
                //               Output - Current charge, Group name, Accumulated Charges, Average Charge, Number of Groups
                double dblMinutesUsed;
                double dblAverageCharge, dblCurrentCharge, dblAccumulatedCharges, dblNumberOfGroups;
                dblAccumulatedCharges = 0;
                dblNumberOfGroups = 1;

                //Input - Gather the data
                try
                {
                    dblMinutesUsed = double.Parse(txtMinutesUsed.Text);

                }
                catch
                {
                    MessageBox.Show("Input was either non-numeric or blank", "Invalid Input!");
                    txtMinutesUsed.Focus();
                    return;
                }

    • //Where the value that you get from?

                //Process - Calculate Current Charge, Accumulated Charges, Number of Groups, and Average charge
                dblCurrentCharge = dblMinutesUsed * rentalRate;
                dblAverageCharge = totalAccumulatedCharges / dblNumberOfGroups;
                dblAccumulatedCharges = dblCurrentCharge + totalAccumulatedCharges;
                dblNumberOfGroups += totalNumberOfGroups;
                totalNumberOfGroups++;
              
                //Output - Display the information

                //Clear all the output listboxes
                lstAccumulatedCharges.Items.Clear();
                lstAverageCharge.Items.Clear();
                lstCurrentCharge.Items.Clear();
                lstGroupName.Items.Clear();
                lstNumberOfGroups.Items.Clear();

                //Display the information
                lstAccumulatedCharges.Items.Add(dblAccumulatedCharges.ToString("n1"));
                lstAverageCharge.Items.Add(dblAverageCharge.ToString("n1"));
                lstCurrentCharge.Items.Add(dblCurrentCharge.ToString("n1"));
                lstGroupName.Items.Add(txtGroupName.Text);
                lstNumberOfGroups.Items.Add(dblNumberOfGroups.ToString("n1"));

            }

    If the post is helpful or answers your question, please mark it as such!
    • Marked As Answer byDivisionBy0 Friday, November 06, 2009 1:28 PM
    •  
  • Friday, November 06, 2009 11:16 AMLouis.fr Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    You never change the value of totalAccumulatedCharges
    • Marked As Answer byDivisionBy0 Friday, November 06, 2009 1:28 PM
    •  
  • Friday, November 06, 2009 4:28 PMDivisionBy0 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks guys. Gave me a bit of a better understanding, and I was able to complete the program.