People who have used VB6 or early code should be able to feel that C # has not seen anything special at the moment, so-called object-oriented. So we need to rewrite the code based on the original, and then compare the pros and cons of the two approaches. We right-click on the project to add a Functionmath class.
?
The reason is called Functionmath, is to say that he specializes in mathematical operations (outside the subtraction root, matrix operations, four-dollar operation, etc. can be considered to be the things that math do, we do this class alone is to expand this class ourselves, and then more and more powerful, After encountering the problem without Baidu search, do not ask people, directly look at their own writing function library is finished. We can also give this class an individual namespace, the so-called namespace namespace is the type of the class is the gate (such as the previous heap of using System.xxx, then these class libraries are all system-owned sub-class, we create a functions class library, we think it is to create a portal, what other projects as long as the reference to this class library, you can use the following method to improve code reuse degree)
?
You can basically copy Form1 references, which are not necessarily used (at least we have added a using System.Windows.Forms this DLL), which makes it easy to execute the MessageBox output at any time we want to see the information (we have namespace it is functions no need to reference ourselves again)
?
This class at the beginning is empty, we do not rush to write, first to understand what this class to do, the first is to complete the subtraction, then it is obvious that need such four methods, and each method input, output can be easily figured out. Note that the method defined in the class is not related to the Form1 interaction, and you should not move the variables inside the Form1 into this functionmath, to keep the class clean and tidy (division dividend cannot be 0 of these illegal situations we should also implement in the custom class, Imperfect places are found to perfect, so that the more complete the class, now can think of where to write where?
?
In addition to subtraction, we can also think of such as common open square, open square root, conversion, angle radian conversion and so on (so obviously, a complete mathematical class will have a lot of methods, we can also add vector operations, such as point multiplication, cross-multiply, to find the angle, you can also add matrix operations, such as the left, right, transpose , in any case, we have learned to build foundational things and are being put into use.
?
This way we can use our own class method to perform the specific function (when using the same time not to judge whether to divide by 0 of these illegal cases, it should be in the custom class perfect). This is the so-called face object thinking method, put a kind of function into a calss inside, everyone should do what is clear, such as Functionmath only responsible for mathematical operations (we will gradually update the functionstring responsible for string processing, Functioncommunication is responsible for communication processing, Functionsystem is responsible for system interaction, etc.), And the Designer.cs is only responsible for the interface layout, in a word is to let a file to represent a class, do one thing (a company when a small time may be a person of technology, sales, finance, personnel things are dry, but the target if the practice of large companies, do technical regardless of sales, each document function, the more clearly the better) , this requires the reader to slowly understand, develop the habit of doing so, although the double-click button directly in the inside write very cool, but that is the way of the face process, if you write tens of thousands of lines of code is too much, you are looking for you to write before the things, the entire project inside the duplicate code, garbage code is everywhere.
?
Although the specific calculations are stripped out (we can constantly refine this functionmath class), but the main program is still very messy, is it possible to make the code more streamlined, such as clicking on a button to perform only one function? This needs to be based on the actual project to think about how to streamline. For example, we are currently writing a simple calculator (may also be done very complicated), we can simply think of the button is divided into three categories, one is the operand (number, decimal point), a class is the operator (subtraction, open square), a class is the result (calculation button), may clear the button can also be used as a class, But it doesn't matter, actually click any button can be classified into a method to execute, so the main interface we can first make such a method (need two parameters, the first is the specific button content, the second is our own definition of button type)
?
Process, I personally think of A and B two operands, C is the final result (regardless of continuous operation), in order to follow the Windows comes with the calculator to differentiate, we will always show two operands and operation results to avoid entering a large amount of data when the previous input is not clear. Then press subtraction this operation symbol button, after switching between A and B (before the operator is pressed, our number keys are always in operation A, after the operator, a is fixed after the number is B) This is a relatively simple logic flow, the reader can also think of there is no better, more reasonable wording. But at least so far, no problem has been identified.
?
To this part at least our entire program structure has been more complete, less than the previous version of the main program 50 lines, while the reader can easily find that the changes will be more convenient than before (a method, change the whole), in the following section, we continue to optimize
?
For more instructional videos and downloads, please pay attention to the following information:
My Youku Space:
Http://i.youku.com/acetaohai123
?
My online forum:
http://csrobot.gz01.bdysite.com/
?
Problem Exchange:
qq:910358960
Email:[email protected]
?
?
?
C # Basic Video tutorial 4.2 How to write a simple calculator