Section 7.5. Shared Methods and Class Math


7.5. Shared Methods and Class Math

As 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


ClassName.methodName(arguments)

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

It is not necessary to add an assembly reference to use the Math class methods in a program, because class Math is located in namespace System, which is implicitly added to all .NET applications.


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.

Figure 7.3. Math class methods.

Method

Description

Example

Abs(x)

returns the absolute value of x

Abs(23.7) is 23.7

Abs(0) is 0

Abs(-23.7) is 23.7

Ceiling(x)

rounds x to the smallest integer not less than x

Ceiling(9.2) is 10.0

Ceiling(-9.8) is -9.0

Cos(x)

returns the trigonometric cosine of x (x in radians)

Cos(0.0) is 1.0

Exp(x)

returns the exponential ex

Exp(1.0) is approximately 2.71828182845905

Exp(2.0) is approximately 7.38905609893065

Floor(x)

rounds x to the largest integer not greater than x

Floor(9.2) is 9.0

Floor(-9.8) is -10.0

Log(x)

returns the natural logarithm of x (base e)

Log(2.7182818284590451) is approximately 1.0

Log(7.3890560989306504) is approximately 2.0

Max(x, y)

returns the larger value of x and y (also has versions for Single, Integer and Long values)

Max(2.3, 12.7) is 12.7

Max(-2.3, -12.7) is -2.3

Min(x, y)

returns the smaller value of x and y (also has versions for Single, Integer and Long values)

Min(2.3, 12.7) is 2.3

Min(-2.3, -12.7) is -12.7

Pow(x, y)

calculates x raised to the power y (xy)

Pow(2.0, 7.0) is 128.0

Pow(9.0, .5) is 3.0

Sin(x)

returns the trigonometric sine of x (x in radians)

Sin(0.0) is 0.0

Sqrt(x)

returns the square root of x

Sqrt(9.0) is 3.0

Sqrt(2.0) is 1.4142135623731

Tan(x)

returns the trigonometric tangent of x (x in radians)

Tan(0.0) is 0.0


Math Class Constants PI and E

Class 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.



Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net