Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Windows Forms
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.
 
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 9-Jul-13 7:52am
Zuviox259
Comments
ThePhantomUpvoter at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
 
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!
  Permalink  
Comments
CHill60 at 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 at 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 at 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 at 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 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
 
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();
 }
  Permalink  
v8

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

  Print Answers RSS
0 OriginalGriff 190
1 PIEBALDconsult 150
2 BillWoodruff 148
3 DamithSL 140
4 Jochen Arndt 125
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 9 Jul 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100