Visual C # 2005 Quick Start Call method

Source: Internet
Author: User
Tags argumentlist continue variables requires tostring window visual studio
The visual| Quick Start method exists to be invoked! Use the method name to invoke a method that requires it to perform its task. If the method is to get information (specified by its arguments), it must provide the information it needs. If the method returns information (specified by its return type), it should somehow capture the information.

   Specify method invocation syntax

In order to invoke a C # method, you need to use the following syntax form:

MethodName (ArgumentList)
The MethodName (method name) must be exactly the same as the name of the method that is invoked. Remember, the C # language is case-sensitive.
The argumentlist (parameter list) is used to provide optional information that will be received by the method. You must provide a parameter value (argument) for each parameter (formal parameter), and each parameter value must be compatible with the type of its corresponding formal parameter. If the method has two or more parameters, you must use commas to delimit different parameters when supplying the parameter values.

Important a pair of parentheses must be included in each method call, even if a method without parameters is invoked.

The Addvalues method is listed below again:

int addvalues (int lefthandside, int righthandside)
{
// ...
}
The Addvalues method has two int parameters, so when calling the method, you must supply two comma-delimited int arguments:

Addvalues (39, 3); Right way
You can also replace the direct amount 39 and 3 with the name of an int variable. The values of these variables are passed as parameter values to the method, for example:

int arg1 = 99;
int arg2 = 1;
Addvalues (Arg1, arg2);
The following is a list of some incorrect addvalues invocation methods:

Addvalues; Compile-time errors, no parentheses
Addvalues (); Compile-time error, no sufficient arguments
Addvalues (39); Compile-time error, no sufficient arguments
Addvalues ("39", "3"); Compile-time error, type error
The Addvalues method returns an int value. This int value can be used anywhere where an int value is available. For example:

result = Addvalues (39, 3); The right-hand operand as an assignment operator
Showresult (Addvalues (39, 3)); argument that is called as another method
In the following exercise, we will continue to use the Mathsoperators application. This time, we'll look at some method calls.

   Research method call

1. Return to methods project. If you have just completed the previous exercise, the project should already be open in Visual Studio 2005, otherwise, from the \microsoft Press\visual CSharp step of the My Documents folder Step\chapter 3\ Open it in the methods subfolder.

2. Displays the Form1.cs code in the Code and Text Editor window.

3. Find the Calculate_click method and observe the first two statements of the method after the try statement and the opening brace.

The two statements are:

int lefthandside = System.Int32.Parse (Lefthandsideoperand.text);
int righthandside = System.Int32.Parse (Righthandsideoperand.text);
The two statements declare two int variables, called Lefthandside and Righthandside respectively. However, the most interesting place is the way in which variables are initialized. In two statements, the parse method of the System.Int32 class is called (System is a namespace, and Int32 is the name of a class in that namespace). The parse method requires a string parameter to be obtained and converts it to an int value. After the two statements are executed, any content that the user enters into the Lefthandsideoperand and Righthandsideoperand text boxes on the form is converted to an int value.

4. Observe the 4th statement of the Calculate_click method (after the if statement and another starting brace):

Calculatedvalue = Addvalues (Lefthandside, righthandside));
The statement calls the Addvalues method to pass the values of the lefthandside and righthandside variables as arguments to the method. The return value of the Addvalues method is stored in the Calculatedvalue variable.

5. Continue to observe the next statement:

Showresult (Calculatedvalue);
The statement calls the Showresult method to pass the value of the Calculatedvalue variable as the method's argument. The Showresult method does not return any values.

6. Find the Showresult method discussed earlier in the Code and Text Editor window. The method has only one statement:

Result. Text = answer. ToString ();
Note that parentheses are also used when calling the ToString method, even if there are no arguments.

Tip to invoke a method that is subordinate to another object, you can attach the object name prefix before the method name. In the example above, an expression answer. ToString () calls the ToString method that is subordinate to the answer object.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.