Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Basically im creating a windows form that asks you to enter 12 numbers and then display them in a text box. Ive done that. But it asks to add all the numbers in the array to be summed up in a total box, as wel as a vat box, min and max. I cannot seem to get the answer to display in the total box. I seemed to have got the number 0 to show when the Calculate button was pressed.But now nothing shows. Here is all the code, how you can help.

C#
public partial class Form1 : Form
    {
        string[] _prices = new string[2];
        int _index;
        string _outputDouble = "";

        double _total = 0.0;

        public void ConvertStringToDouble()
        {
            double[] _prices = new double[_index];

            for (_index = 0; _index < 2; _index++)
            {
                _prices[_index] = double.Parse(_outputDouble);
            }
        }

        private void Total()
        {
            double[] _prices = new double [_index];
            _total = 0.0;

            foreach (double price in _prices)
            {
                _total = _total + price;
            }

    //        _total =

            totalDisplay.Text = _total.ToString();

        }

        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void enterPriceButton_Click(object sender, EventArgs e)
        {
            if (_index < 2)
            {
                _prices[_index] = priceTextBox.Text;
                priceTextBox.Text = "";
                _index++;
            }
            else
            {
                enterPriceButton.Enabled = false;
                Display();
            }
      }


        private void Display()
        {
            _outputDouble = "";

            foreach (string price in _prices)
            {
                _outputDouble = _outputDouble + price + "\r\n";
            }

            displayTextBox.Text = _outputDouble;

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void displayTextBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void totalDisplay_Click(object sender, EventArgs e)
        {

        }

        private void calcTotalButton_Click(object sender, EventArgs e)
        {
            calcTotalButton.Enabled = false;
            Total();
        }
    }
Posted
Comments
[no name] 9-Jul-13 13:09pm    
At least part of your problem lies in:
double[] _prices = new double [_index];
_total = 0.0;
foreach (double price in _prices)
You are creating a new _prices array that contains nothing so there is nothing to foreach through and your _total remains 0.

I removed code that was not needed and ensured proper scope of variables. I did not add code to validate that the data in the textbox was actually numeric and could be successfully converted to a double data type. Also, I did not add code to limit the number of entries to twelve. A complete application would do those things.


Tested in C# Visual Studio 2012:

C#
double[] _prices = new double[11];     // Declare room for 12 double items
int _index=0;                          // 0-11 - Which index of the 12 items?

 private void Total()
 {
     double _total = 0.0;
     foreach (double price in _prices)
     {
         _total = _total + price;
     }
     totalDisplay.Text = _total.ToString("###,###,###,###.00");
 }

 private void enterPriceButton_Click(object sender, EventArgs e)
 {
     _prices[_index] = double.Parse(priceTextBox.Text);
    priceTextBox.Text = "";
    // Add the latest price to the end of the textbox
    displayTextBox.Text += _prices[_index].ToString("###,###,###,###.00") + "\r\n";
    _index++;
}

 private void calcTotalButton_Click(object sender, EventArgs e)
 {
     calcTotalButton.Enabled = false;
     Total();
 }
 
Share this answer
 
v8
There are significant problems here, the first of which seems to be that you have no understanding of variables and their scope - that's not a major problem as it's pretty simple really.

Let's ignore computers for a moment or two and think about friends. Suppose you have a friend called Bob, and he comes round to see you - so you sit him down in the living room, and go to get him a cup of tea. Unfortunately, Bob is deaf, so he can only "hear" you when he is in the same room - because he lip reads. As soon as you are in the kitchen, Bob can't see you, so he can't communicate any information to you - such as the number of sugars he would like. His scope is limited to the living room and as soon as you leave it you can't talk to him. So, you go back to the living room to ask him about the sugar, and surprise! It's a different Bob who knows nothing about the tea!

What's happened here is exaggerated, but it is what happens with methods: any variables you declare within the method are only accessible and only exist while the method is being executed. Poor Bob - you killed him when you went to get the tea, and created a new one when you came back for sugar...

If you want Bob to be persistent, then he needs to have a scope which covers the whole house - he needs to get his hearing back. This means declaring the Bob Variable at a class level instead of in a method:

C#
class House
   {
   MyFriend Bob = new MyFriend();
   void LivingRoom()
      {
      Bob = MyFriend.WantsTea;
      }

   void Kitchen()
      {
      if (makingTea)
         {
         int sugars = Bob.SugarCount;
         ...
         }
      }
   }

Because Bob is declared outside any method, his scope is the whole of the House class - and any routine in the House class can access him (unless you declare a new variable called Bob inside a method in which case that variable takes precedence).

So, look at your code, and see which scope Bob (AKAK _prices) has in each case!
 
Share this answer
 
Comments
CHill60 9-Jul-13 14:41pm    
Interesting analogy! The concept of scope is so simple and yet often difficult to get across - I may have to "borrow" your deaf friend in future :) +5
OriginalGriff 9-Jul-13 14:46pm    
You're welcome to him!
It's a PITA to explain because there seems so little in the real world that directly mirrors it - unlike most of computing. There is a lot of stuff that does, but it's not at all obvious (particularly the new instance part) - I'm not sure Bob is the perfect example but he is easy to grasp I hope.
Zuviox 9-Jul-13 14:50pm    
Thank you so much. The reason I hadn't created a class was because this is a uni assignment and we haven't been introduced to classes yet. That believe it or not is the next worksheet and this _prices task is the last task on the functions worksheet :P
But your analogy was very helpful thank you so much :)
OriginalGriff 9-Jul-13 15:03pm    
:laugh:
You have created a class:
public partial class Form1 : Form

Everything (and I do mean everything) if part of a class in C#.

Glad it helped!
Zuviox 9-Jul-13 16:27pm    
No i meant we haven't been introduced to them, obviously ive got classes. But they are just default when opened in visual studio. :P

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900