alright i found a page on handling keyboard input on MSDN. so i made my little keystroke handler and it handles all my strokes for 0-9 plus the operations (+, -, *, /, % but how do i handle the enter key?
i dont have a C# instructor. i am my own teacher everything i do with this language i do on my own. so no one has told me anything (except what you guys post to all my dumb question, thanks for doing that by the way). the computer science teacher at my high school lent me a book on C# but it does not cover handling raw keystrokes in it. i've looked. the book is "Microsoft Visual C# 200 Step by Step" by "John Sharp". its a nice book for helping you use text boxes and stuff but i am using only one text box and the user does not input any form of data into it. only the program has the accesibility to do that.
You need to read up on the KeyPress event handler. I would also recommend you look at Regex as it is REALLY helpful later on, its very simple to get used to and also you can print a "cheat sheet" about it for later use. Still use mine
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.
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
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].
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.
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.
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)?
[ class Calculator....]..........NOTE: This class is probably called Form1 in your project.
- m_operation ----------------------------------->[ abstract class Operation ]
// methods & Event handlers + int AcceptOperand(int operand).........NOTE: Also abstract.
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:
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.)
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.
Last edited by TheGreatCthulhu; October 12th, 2010 at 06:18 AM.
well thanks everyone for helping me. i have almost finished the calculator. i know its way beginner but i found that the concepts behind it are very important and help me with my future C# plans(XNA). i am slowly working my way into this book by John Sharp and i am now up to the point of getting the ADO.NET going. i need to work on finishing my calculator before i jump into that project though. keep up on this post or any other posts i leave here i will almost always be in need of assistance with my code. i have calculator working it now reads input from the keyboard(including % which is nice. i have one little problem with my keyboard reading though. HOW ON EARTH DO YOU READ THE ENTER KEY!? i cant get it to work and the keypress doesnt allow more than one character between '' i tried to put 'CR' but it threw the error of to many characters. so what is the ASCII code for the enter key and how do i make the enter key be only one thing and not a click on the last button used? this will almost finish the project.
Long is way to XNA
OK, you might figure out how to do some of the simpler stuff, but beyond that you'll need experience with stuff that doesn't necessarily have to do much (or anything) with XNA - I'm talking about the need to understand how different parts of code work together, then general stuff related to design, the ability to understand how other people have designed the code you want to use, and the ability to find and understand information.
The thing is, no API or framework will do all the things for you: you need to build on top of them.
But don't get me wrong, I by no means want to discourage you. Be persistent!
As for the enter key, are you using the KeyPress event? (You shouldn't.)
See the very first line of my previous post, with the two links.
BTW, you can't say 'CR' in C# because the ' ' denote a char literal, and CR are two chars, which technically makes it a string. (Note that the same applies in C++.)
There's something called escape sequences (C++ has these too), one of which is '\r' (NOTE: this is treated as a single char) and it corresponds to carriage return - but this wouldn't work either, because the KeyPress event is not raised by noncharacter keys.
Also, before you jump into ADO.NET, or any other stuff, i recommend that you familiarize at least with the basic concepts of OO-programming, such are inheritance, abstraction, polymorphism, encapsulation, composition etc... These are all related with how classes cooperate, and any C# book should talk about these topic at least a little. You'll certainly benefit from this knowledge later on.
I'd like to see how you coded the calculator app - if you would like to share?
like i said XNA is a future plan(mainly a summer hobby and then on to college wish me luck there i still got a year of high school ) i did read your last post more clearly and did find the links. i skipped them i didnt even see the first line so thanks for telling me that. i looked at the overrides for the KeyPress event and i do believe that will be what is needed to handle the very thing i need. also will that completely override the enter key for the calculator? so if i click the button for 3 but then hit enter will it click another 3 or just the equal? i hope the equal. and i looked at the escape sequences. i remember those from C++ console programming. i used them quite a bit especially for writing to .txt files. i will post the code here once i finish the enter key and the decimals (needs counter and some more code) but i have no problem showing the code for critiques.
I understand that. I have to work on that some more. Anyway I am using the whole KeyDown method I guess you could call it but it doesn't seem to work the way I need it to. How do I switch "KeyEventArgs e" to a string or int?
Error 1 Cannot implicitly convert type 'System.Windows.Forms.Keys' to 'int'. An explicit conversion exists (are you missing a cast?) F:\CS_Calculator\CS_Calculator\Calculator.cs 383 22 CS_Calculator
Error 2 Cannot implicitly convert type 'System.Windows.Forms.Keys' to 'int'. An explicit conversion exists (are you missing a cast?) F:\CS_Calculator\CS_Calculator\Calculator.cs 388 22 CS_Calculator