Previous Page
Next Page

Calling Methods

Methods exist to be called! You call a method by name to ask it to perform its task. If the method requires information (as specified by its parameters), you must supply the information requested. If the method returns information (as specified by its return type), you should arrange to capture this information somehow.

Specifying the Method Call Syntax

The syntax of a C# method call is as follows:

methodName ( argumentList )

Here is the addValues method again:

int addValues(int leftHandSide, int rightHandSide)  
{  
    // ...  
}

The addValues method has two int parameters, so you must call it with two comma-separated int arguments:

addValues(39, 3);     // okay

You can also replace the literal values 39 and 3 with the names of int variables. The values in those variables are then passed to the method as its arguments, like this:

int arg1 = 99;
int arg2 = 1;
addValues(arg1, arg2);

If you try to call addValues in some other way, you will probably not succeed, for the reasons described in the following examples:

addValues;            // compile time error, no parentheses 
addValues();          // compile time error, not enough arguments 
addValues(39);        // compile time error, not enough arguments 
addValues("39", "3"); // compile time error, wrong types

The addValues method returns an int value. This int value can be used wherever an int value can be used. Consider these examples:

result = addValues(39, 3);    // on right hand side of an assignment 
showResult(addValues(39, 3)); // as argument to another method call

The following exercise continues using the MathsOperators application. This time you will examine some method calls.

Examine method calls
  1. Return to the Methods project. (This project is already open in Visual Studio 2005 if you're continuing from the previous exercise. If you are not, open it from the \Microsoft Press\Visual CSharp Step by Step\Chapter 3\Methods folder in your My Documents folder.

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

  3. Locate the calculate_Click method, and look at the first two statements of this method after the try statement and opening curly brace. (We will cover the purpose of try statements in Chapter 6, “Managing Errors and Exceptions.”)

    The statements are as follows:

    int leftHandSide = System.Int32.Parse(leftHandSideOperand.Text); 
    int rightHandSide = System.Int32.Parse(rightHandSideOperand.Text);

    These two statements declare two int variables called leftHandSide and rightHandSide. However, the interesting parts are the way in which the variables are initialized. In both cases, the Parse method of the System.Int32 class is called (System is a namespace, Int32 is the name of the class in this namespace). This method takes a single string parameter and converts it to an int value. These two lines of code take whatever the user has typed into the leftHandSideOperand and rightHandSideOperand TextBox controls on the form and converts them into int values.

  4. Look at the fourth statement in the calculate_Click method (after the if statement and another opening curly brace):

    calculatedValue = addValues(leftHandSide, rightHandSide));

    This statement calls the addValues method, passing the values of the leftHandSide and rightHandSide variables as its arguments. The value returned by the addValues method is stored in the calculatedValue variable.

  5. Look at the next statement:

    showResult(calculatedValue);

    This statement calls the showResult method, passing the value in the calculatedValue variable as its argument. The showResult method does not return a value.

  6. In the Code and Text Editor window, find the showResult method you looked at earlier.

    The only statement of this method is this:

    result.Text = answer.ToString();

    Notice that the ToString method call uses parentheses even though there are no arguments.

    TIP
    You can call methods belonging to other objects by prefixing the method with the name of the object. In the previous example, the expression answer.ToString() calls the method named ToString belonging to the object called answer.

Previous Page
Next Page