You can add methods as members to your classes. Methods represent actions your class can take. Methods generally come in two varieties: those that return a value (functions in Visual Basic) and those that do not return a value (Subs in Visual Basic).
The following code shows an example of both kinds of methods:
Visual Basic .NET
Public Sub MySub()
MessageBox.Show("This is a non-value returning method")
End Sub
Public Function Add(ByVal first as Integer, ByVal second as _
Integer)
Dim Result as Integer
Result = first + second
End Function
Visual C# makes no distinction between methods that return a value and methods that do not. In either case, you must specify the return value type. If the method does not return a value, its return type is void. Here are examples of C# methods:
Visual C#
public void myVoidMethod()
{
MessageBox.Show("This method doesn't return a value");
}
public int Add(int first, int second)
{
int Result;
Result = first + second;
}
Calling Methods:
A method does not execute until it is called. You can call a method by referencing its method name along with any required parameters.
Visual Basic .NET
' This line calls the Rotate method, with two parameters
Rotate(45, "Degrees")
Visual C#
// This line calls the Rotate method, with two parameters
Rotate(45, "Degrees");
The Main method is a special case. It is called upon initiation of program execution. Destructors, another special case, are called by the run time just prior to the destruction of an object. Constructors, a third special case, are executed by an object during its initialization.
Method Variables:
Variables declared within methods are said to have method scope. This means that once the method completes execution, they are destroyed and their memory reclaimed. They are said to have gone out of scope.
Variables within smaller divisions of methods have even more limited scope. For example, variables declared within a For-Next (for) loop are accessible only within the loop.
Although this variable persists in memory, it is still only available during execution of this method. You would use a static variable for a method that needed to keep track of how many times it had been called.
Parameters:
A method can take one or more parameters. A parameter is an argument that is passed to the method by the method that calls it. Parameters are enclosed in parentheses after the method name in the method declaration, and types must be specified for parameters.
This method requires two parameters. A String parameter, which is given the local name name, and a Byte parameter, which is given the local name age. These variables have scope only for the duration of the method and cannot be used after the method returns.
Parameters can be passed in two ways: by value or by reference. In the .NET Framework, parameters are passed by value by default. This means that whenever a parameter is supplied, a copy of the data contained in the variable is made and passed to the method.
Any changes made in the value passed to the method are not reflected in the original variable. Although it is the default setting, you can explicitly indicate that a variable be passed by value in Visual Basic with the ByVal keyword.
When parameters are passed by reference, on the other hand, a reference to the memory location where the variable resides is supplied instead of an actual value. Thus, every time the method performs a manipulation on that variable, the changes are reflected in the actual object. To pass a parameter by reference in Visual Basic .NET, you use the keyword ByRef. In Visual C#, the keyword ref is used.
If your parameter is a reference type, it makes no difference if the parameter is passed by value or by reference—the behavior will be the same. In both cases, any manipulations done on the parameter will be reflected in the object passed as a parameter.
Output Parameters:
In Visual C#, you can also use output parameters. This feature is not available in Visual Basic .NET. An output parameter is a parameter that is passed from a called method to the method that called it—that is, the reverse direction.
Output parameters are useful if you want a method to return more than a single value. An output parameter is specified by using the out keyword. Output parameters are always passed by reference and do not need to be initialized before use.
Optional Parameters:
In Visual Basic .NET, you are able to specify optional parameters for your methods. This feature is not available in Visual C#. You specify a parameter as optional using the Optional keyword. Optional parameters must be the last parameters in a method declaration, and you must supply default values for optional parameters.
Constructors and Destructors:
The constructor is the first method that is run when an instance of a type is created. In Visual Basic, the constructor is always Sub New. In Visual C#, it is a method with the same name as the class. You use a constructor to initialize class and structure data before use. Constructors can never return a value, and can be overridden to provide custom intitialization functionality . A constructor can also contain calls to other methods.
related post
DAY 1 MICROSOFT DOT NET FRAME WORK
DAY 2 MICROSOFT DOT NET BASE CLASS LIBRARY
DAY 3 MICROSOFT DOT NET CLASSES AND STRECTURES
DAY 4 METHODS IN FRAME WORK
DAY 5 INPUT VALIDATIONS IN DOT NET PART ONE
DAY 6 INPUT VALIDATIONS IN DOT NET PART TWO
DAY 7 DATA TYPES IN DOT NET
DAY 8 DATA TYPES IN DOT NET PART TWO
DAY 9 IMPLEMENTING PROPERTIES IN DOT NET
DAY 10 DELEGATES AND EVENTS
No comments:
Post a Comment