7.5. Shared Methods and Class MathAs you know, every class provides methods that perform common tasks on objects of the class. For example, to display the grade information in a GradeBook object, we defined and called method DisplayGradeReport (Section 6.5). Recall that when we call GradeBook methods such as DisplayGradeReport, we first need to create a GradeBook object. Although most methods of a class execute in response to method calls on specific objects, this is not always the case. Sometimes a method performs a task that does not depend on the contents of an object. Such a method applies to the class in which it is declared and is known as a Shared method or a class method. It is not uncommon for a class to contain a group of convenient Shared methods to perform common tasks. For example, recall that we use method WriteLine of class Console throughout this book; method WriteLine is a Shared method. Methods that are declared in modules are Shared by default. To declare a Shared method in a class, place the Shared modifier before the keyword Sub or Function in the method's header. To call a Shared method, specify the name of the class or module in which the method is declared, followed by the dot (.) separator and the method name, as in
We use various Math class Shared methods here. Class Math provides a collection of methods that enable you to perform common mathematical calculations. For example, you can calculate the square root of 900.0 with the Shared method call Math.Sqrt(900.0) which evaluates to and returns 30.0. Method Sqrt takes an argument of type Double and returns a result of type Double. To output the value of the preceding method call in the command prompt, you could write Console.WriteLine(Math.Sqrt(900.0)) In this statement, the value that Sqrt returns becomes the argument to method WriteLine. Note that we did not create a Math object before calling method Sqrt. Most Math class methods are Shared and are therefore called by preceding the name of the method with the class name Math and a dot (.) separator. Software Engineering Observation 7.5
Method arguments may be constants, variables or expressions. If c = 13.0, d = 3.0 and f = 4.0, then the statement Console.WriteLine(Math.Sqrt(c + d * f)) calculates and prints the square root of 13.0 + 3.0 * 4.0 = 25.0namely, 5.0. Fig. 7.3 summarizes several Math class methods. In the figure, x and y are of type Double. Methods Min and Max have overloaded versions for several types. We discuss method overloading in Section 7.17.
Math Class Constants PI and EClass Math also declares two commonly used mathematical constants: Math.PI and Math.E. The constant Math.PI (3.14159265358979323846) is the ratio of a circle's circumference to its diameter. The constant Math.E (2.7182818284590452354) is the base value for natural logarithms (calculated with Shared Math method Log). These values are declared in class Math with the modifiers Public and Const. Making them Public allows you to use the values in your own classes. Keyword Const declares a constanta value that cannot be changed after the variable is initialized. Both PI and E are declared Const because their values never change. Constants are implicitly Shared, so they can be accessed via the class name Math and a dot (.) separator, just like class Math's methods. Recall from Section 4.5 that when each object of a class maintains its own copy of an attribute, the variable that represents the attribute is also known as an instance variableeach object (instance) of the class has a separate instance of the variable in memory. There are also variables for which each object of a class does not have a separate instance of the variable. That is the case with Shared variables. When objects of a class containing Shared variables are created, all the objects of that class share one copy of the class's Shared variables. Together the Shared variables and instance variables represent the so-called fields of a class. You will learn more about Shared members in Section 9.10. |