You can create a constant variable in a class. To do this, type the const keyword to the left of the variable. When declaring a constant, you must initialize it with an appropriate constant value. If a class contains fields and methods, the (non-static) field members are automatically available to the method(s) of the class, even fields that are private. When accessing a field or a method from another method of the class, to indicate that the member you are accessing belongs to the same class, you can precede it with the this member and the period operator. When using the this member variable, you can access any member of a class within any method of the same class. There are rules you must observe when using this:
<script runat="server"> public class House { public void SetPropertyNumber(long number) { } } </script> In the body of the method, you may or may not use the value of the argument. Otherwise, you can manipulate the supplied value as you see fit. When calling a method that takes an argument, you must supply a value for the argument; otherwise you would receive an error. Also, you should/must supply the right value; otherwise, the method may not work as expected and it may produce an unreliable result. Here is an example: <%@ Page Language="C#" %> <html> <head> <script runat="server"> public class House { public void SetPropertyNumber(long number) { } } </script> <title>Exercise</title> </head> <body> <% House property = new House(); property.SetPropertyNumber(283795); %> </body> </html> A method that takes an argument can also declare its own local variable(s). A method can take more than one argument. When defining such a method, provide each argument with its data type and a name. The arguments are separated by a comma.
When calling a methods that takes one or more arguments, we made sure we provided the necessary value. This is because an argument is always required and the calling method must provide a valid value when calling such a method. If you supply the argument using its name, the compiler only makes a copy of the argument's value and gives it to the calling method. Although the calling method receives the argument 's value and can use it in any way, it cannot (permanently) alter it. You can call a method to modify the value of a passed argument if you find it necessary. If you want the calling method to modify the value of a supplied argument and return the modified value, you should pass the argument using its reference.To pass an argument as a reference, when defining and when calling the method, precede the argument's data type with the ref keyword. You can pass 0, one, or more arguments as reference in the program or pass all arguments as reference. The decision as to which argument(s) should be passed by value or by reference is based on whether or not you want the called method to modify the argument and permanently change its value. Another option consists of passing an argument using the out keyword. Here is an example: <script runat="server"> public class House { public void SetPropertyNumber(out long number) { } } </script> If you pass an argument with out, any modification made on the argument would be kept when the method ends. When calling a method that takes an out argument, precede the argument with the out keyword. Here is an example: <%@ Page Language="C#" %> <html> <head> <script runat="server"> public class House { public void SetPropertyNumber(out long number) { number = 283795; } } </script> <title>Exercise</title> </head> <body> <% House property = new House(); long nbr; property.SetPropertyNumber(out nbr); %> </body> </html>
Method overloading consists of using the same name for more than one method. Of course, there are rules you must observe:
|
|
||||||||||||||||||||||
|