• No results found

ADD STATIC METHODS

In document C # (Page 136-140)

ADD STATIC METHODS

Add Method Add Class1

int

void StaticMethod

Á

Click the Class View tab.

Click the plus sign ( ) next to the Method name.

°

Click the plus sign ( ) next to the {} Method name.

·

Right-click the class name.

Click Add ➪ Add Method.

■ The C# Method Wizard appears.

Type the method name in the Method name field.

Note: The Method signature field at the bottom reflects the changes to the method code as you type information into the wizard fields.

If you need to return more than one variable from your static method, you can do so using the params keyword.

TYPE THIS:

using System;

public class Params {

public static void Parameter(params int[] list) {

for ( int x = 0 ; x < list.Length ; x++

)

Console.WriteLine(list[x]);

Console.WriteLine();

}

public static void Main() {

Parameter(10, 15, 20);

} }

RESULT:

10 15 20

CONTINUED

void

Class1.cs int

±

Type the parameter name in the Parameter name field.

¡

Click Add.

■ The added parameter name appears in the Parameter list field.

Click to select a method modifier from the Method modifiers check box area.

£

Click Finish.

■ The static method code appears in the parent window.

¢

Move the static method code above the Main method code.

Type the Record class code that establishes variables and static methods for adding to the number of records.

C

# uses simple names for accessing many different elements in a C# project, and methods are no different. However, if you have a static method then how you program static methods and other static

information in your method determines if you can use simple names or not.

Simple names for a variable can be just one letter, such as x.

When you declare variables and associate them with value types, the methods you include those declarations in determine whether your program can process those variables. For example, you can declare two variables of integers with the simple names a and b, with a declared as a non-static member and b declared as a static member.

If you place the two variables in a non-static method and evaluate them later in your class, you will have no trouble with your evaluation. However, if you put those two variables in a static method you will only be able to evaluate the static variable b because a static method cannot access a non-static variable.

If you decide to plunge ahead anyway and try to evaluate a non-static variable in a static method, you will find that the MDE window will protest that action and your program will not compile until you fix the problem.

ADD STATIC METHODS

ADD STATIC METHODS (CONTINUED)

§

Type the Main method that lets the user input values and outputs the results.

Press the F5 key.

■ Type information at the prompts and the output appears.

Save the program as the filename.

You can reference a static method in what Visual Studio .NET refers to as a member-access format. The member-access format contains the full version of the type and its associated identifier. The member-access format comes in the form of the type, a period, and then the identifier. For example, you can have the member access type int.number.

C# and the Visual Studio .NET suite do not force you to use the member-access format because many of the access types have aliases that C#

refers to. If you reference your static method (or any other static member) in member-access form you must do so in the form E.M. The E must stand for the type that your method uses, not the object. The M stands for the type identifier. For example, if your method is of the type integer with the name NumberOne, then the member access form is int.NumberOne.

Visual C# Projects

Console Applications

Click Start ➪ Programs ➪ Microsoft Visual Studio .NET 7.0 ➪ Microsoft Visual Studio .NET 7.0.

■ The Start page appears.

¤

Click New Project.

■ The New Project window appears.

Click the Console Application icon in the Templates pane.

Type a name for the file.

ˇ

Click OK.

T

he non-static status is the default for all methods.

Non-static methods, also called instance methods, rely on an instance of the class — that is, the non-static method relies on the information it receives from an object generated by the class. Once the non-static method receives that object it provides the object with its

implementation instructions and sends the object back out into the class for further processing.

The non-static method is best if you know that the class will generate an object for the method. If you create a method in an inherited class, then the non-static method is the only

choice. A static method belongs to its class, but a non-static method can take objects from inheriting classes. You can also set non-static methods to override or be overridden by other non-static methods in other inherited classes in your class family or from the base class.

Your non-static method does not accept objects

automatically. You must tell the method that you want to accept the value by using the this keyword. When you use the keyword this in your method, the referenced object receives a type that matches the object type and a value that acts as a reference to the object.

In document C # (Page 136-140)

Related documents