See this & this.
Quote:
Originally Posted by
rockking
i fixed my own error by reading it in the post.
I'm glad you figured it out on your own. About what you said earlier - that you didn't understand what "current context" meant: it just told you that a variable or class or anything else with that name was never defined in the context of that function (not anywhere in the scope of the function, or in the enclosing scope). The function didn't "know" about it.
Quote:
Originally Posted by
rockking
here is the actual method for calculating. obviously its primitive at the moment (only supports two operands and one operation) but its a start
Code:
gbl_result = gbl_operand[0] + gbl_operation[0] + gbl_operand[1];
gbl_result -= gbl_operation[0];
txtShow.Text = gbl_result.ToString();
now could i make a for loop to subtract the values of the operations? so it would look like this
Code:
gbl_result = gbl_operand[0] + gbl_operation[0] + gbl_operand[1];
for(int i = 0; i < gbl_operation.Length; i++)
{
gbl_result -= gbl_operation[i];
}
txtShow.Text = gbl_result.ToString();
would that be technically the proper way to do it?
I don't think it's going to work, not like that. I don't think you're clear about how arrays should be used. BTW, you should be performing operations on operands, so I'm not sure what you tied to achieve with gbl_result -= gbl_operation[i].
Quote:
Originally Posted by
rockking
so i have my calculator and it support up to 11 operands. each conataining at most 9 values.
What do you mean by "up to 11 operands. each conataining at most 9 values"? Like up to 9 digits? Or what?
For starters, I recommend that you try and get it to work with simply using a variable of type double. Don't worry about the digits for the time being - this would unnecessarily complicate matters somewhat, and it wouldn't contribute to your understanding of the language.
Quote:
Originally Posted by
rockking
obviously i dont want a switch statement or a bunch of else clauses. i will have to post some code here on what i am trying to do and i will list the errors that will most likely come along with it.
Quote:
Originally Posted by
rockking
here is a better way of putting. basically can i have an array value set to a character and then when i hit a button to run a specific method it reads that array and says "thats for adding" and it adds but then it runs and goes "thats subtract" and i hope you get the idea by now i will post the code for what i wanna accomplish.
But when the method 'says "thats for adding" and it adds but then it runs and goes "thats subtract"...', that precisely implies a bunch of if-else clauses :)
But, I'm glad you asked, because this is a perfect opportunity to introduce some concepts that will show you how classes and objects actually work together, while keeping it simple at the same time. I'm saying this because the calculator example is simple enough, and can be used to show these concepts without it feeling ill-contrived.
Can you implement something like this (more explanation below)?
Code:
[ class Calculator....]..........NOTE: This class is probably called Form1 in your project.
------------------------
// fields...
- m_currentSum
- m_currentEntry
- m_operation ----------------------------------->[ abstract class Operation ]
------------------------ ---------------------------------
// methods & Event handlers + int AcceptOperand(int operand).........NOTE: Also abstract.
------------------------ ---------------------------------
/\
|
(subclass of)
|
......[class AddOperation]-----------------------+
. |
......[class MultiplyOperation]------------------+
.
.
.
NOTE: (-), (/) and (%) can be implemented using
AddOperation & MultiplyOperation, but you can
implement them separately if you want.
Side NOTE: - denotes 'private', + denotes 'public'
So, the big deal here is what's called inheritance and composition.
Inheritance models the "is a (kind of)" relation, as in "AddOperation is a (kind of) Operation".
Composition, without getting into details, simply means that Operation is somehow a part of, or is at leas referenced by the Calculator.
These two will enable you to avoid the if-else clauses.
Assuming that the Calculator class is the Windows Form where you implemented the calculator UI, it would contain a bunch of handler methods for the buttons. When a number is entered it is first stored in m_currentEntry, and m_currentSum initialy has the same value. When the user presses, say [+], the [+] button handler will set m_operation to an instance of AddOperation (the first operand (m_currentSum) is passed at this point). Then, when the second number is entered, it is stored to m_currentEntry, and finaly, when [=] is pressed, the [=] button handler will simply say:
m_currentSum = m_operation.AcceptOperand(m_currentEntry);
Note that m_operation is of the type Operation, and it doesn't "know" what specific kind of operation it is. It just works (because the subclasses override AcceptOperand(), which does the actual calculation).
Bear in mind that this is obviously designed for illustrative purposes.
(Some would probably say that this is an overkill for a simple calculator app.)
The benefits:
- You can modify the Calculator and Operation-classes independently.
- You can add new types (subclasses) of Operation.
- You can change an existing implementation of an OperationXYZ class without ever having to touch the Calculator class, assuming that the interface stays the same.
For example, you can later decide to add an SqrtOperation that would work on a single operand, in which case it could be initialized with the default (parameterless) constructor. Or you can add RaseToPowerOperation. Or you can, after some changes, implement a CompositeOperation that would link several operations together, thus performing some custom processing of the initial input.
These are slightly advanced topics, and I suggest that, once you've learned the basic syntax and language constructs enough, you start learning something about OO programming, inheritance, composition, and such...
A word of waring: inheritance is powerful, but it shouldn't be overused - prefer composition when possible. Actually, make sure you thoroughly understand the concepts before you decide to use one or the other.
Also, check out the Math class, you will want to use it.