30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 6 Procedures 195<br />

Method Description Example<br />

Sqrt(x) returns the square root of x Sqrt(9.0) is 3.0<br />

Sqrt(2.0) is 1.4142135623731<br />

Tan(x) returns the trigonometric<br />

tangent of x (x in radians)<br />

Tan(0.0) is 0.0<br />

Fig. Fig. 6.7 6.7 Math class methods (part 2 of 2).<br />

Software Engineering Observation 6.9<br />

It is not necessary <strong>to</strong> add an assembly reference <strong>to</strong> use the Math class methods in a program,<br />

because class Math is located in namespace System, which is implicitly added <strong>to</strong> all console<br />

applications. 6.9<br />

6.6 Argument Promotion<br />

An important feature of procedure definitions is the coercion of arguments (i.e., the forcing<br />

of arguments <strong>to</strong> the appropriate data type so that they can be passed <strong>to</strong> a procedure). <strong>Visual</strong><br />

<strong>Basic</strong> supports both widening and narrowing conversions. Widening conversion occurs<br />

when a type is converted <strong>to</strong> another type (usually one that can hold more data) without losing<br />

data, whereas a narrowing conversion occurs when there is potential for data loss during<br />

the conversion (usually <strong>to</strong> a type that holds a smaller amount of data). Figure 6.8 lists<br />

the widening conversions supported by <strong>Visual</strong> <strong>Basic</strong>.<br />

For example, the Math class method Sqrt can be called with an Integer argument,<br />

even though the method is defined in the Math class <strong>to</strong> receive a Double argument. The<br />

statement<br />

Console.Write(Math.Sqrt(4))<br />

correctly evaluates Math.Sqrt(4) and prints the value 2. <strong>Visual</strong> <strong>Basic</strong> promotes (i.e.,<br />

converts) the Integer value 4 <strong>to</strong> the Double value 4.0 before the value is passed <strong>to</strong><br />

Math.Sqrt. In this case, the argument value does not correspond precisely <strong>to</strong> the parameter<br />

type in the method definition, so an implicit widening conversion changes the value <strong>to</strong><br />

the proper type before the method is called. <strong>Visual</strong> <strong>Basic</strong> also performs narrowing conversions<br />

on arguments passed <strong>to</strong> procedures. For example, if String variable number contains<br />

the value "4", the method call Math.Sqrt(number) correctly evaluates <strong>to</strong> 2.<br />

<strong>How</strong>ever, some implicit narrowing conversions can fail, resulting in runtime errors and<br />

logic errors. For example, if number contains the value "hello", passing it as an argument<br />

<strong>to</strong> method Math.Sqrt causes a runtime error. In the next section, we discuss some<br />

measures the programmer can take <strong>to</strong> help avoid such issues.<br />

Common <strong>Program</strong>ming Error 6.7<br />

When performing a narrowing conversion (e.g., Double <strong>to</strong> Integer), conversion of a<br />

primitive-data-type value <strong>to</strong> another primitive data type might change the value. Also, the<br />

conversion of any integral value <strong>to</strong> a floating-point value and back <strong>to</strong> an integral value could<br />

introduce rounding errors in<strong>to</strong> the result. 6.7

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!