Microsoft Visual Basic 2005 Step by Step (Step by Step (Microsoft))

Working with Methods in the Microsoft .NET Framework

Now and then you'll want to do a little extra number crunching in your programs. You might need to round a number, calculate a complex mathematical expression, or introduce randomness into your programs. The math methods shown in the following table can help you work with numbers in your formulas. These methods are provided by the Microsoft .NET Framework, a class library that lets you tap into the power of the Microsoft Windows operating system and accomplish many of the common programming tasks that you need to create your projects. The .NET Framework is a major feature of Visual Studio that is shared by Visual Basic, Microsoft Visual C++, Microsoft Visual C#, Microsoft Visual J#, and other tools in Visual Studio. It's an underlying interface that becomes part of the Windows operating system itself, and it is installed on each computer that runs Visual Studio programs.

The .NET Framework is organized into classes that you can include by name in your programming projects by using the Imports statement. The process is quite simple, and you'll experiment with how it works now by using a math method in the System.Math class of the .NET Framework.

What's New in Microsoft .NET Framework 2.0?

Visual Studio 2005 includes a new version of the .NET Framework—Microsoft .NET Framework 2.0. Version 2.0 adds support for 64-bit extensions to 32-bit processors (AMD64 chips and Intel processors with EM64T technology). The 2.0 Framework also adds new classes that provide additional functionality (greater access to file systems, printers, and the Windows registry), and new classes that make it easier for you to write programs by providing a higher level of “encapsulation” for existing objects and resources. Many of the improvements in the .NET Framework will come to you automatically as you use Visual Basic 2005, and some will become useful as you explore advanced programming techniques.

The following table offers a partial list of the math methods in the System.Math class. The argument n in the table represents the number, variable, or expression you want the method to evaluate. If you use any of these methods, be sure that you put the statement

Imports System.Math

at the very top of your form's code in the Code Editor.

Method

Purpose

Abs(n)

Returns the absolute value of n.

Atan(n)

Returns the arctangent, in radians, of n.

Cos(n)

Returns the cosine of the angle n. The angle n is expressed in radians.

Exp(n)

Returns the constant e raised to the power n.

Sign(n)

Returns -1 if n is less than 0, 0 if n is 0, and +1 if n is greater than 0.

Sin(n)

Returns the sine of the angle n. The angle n is expressed in radians.

Sqrt(n)

Returns the square root of n.

Tan(n)

Returns the tangent of the angle n. The angle n is expressed in radians.

Use the System.Math class to compute square roots

  1. On the File menu, click New Project.

    The New Project dialog box appears.

  2. Create a new Visual Basic Windows Application project named My Framework Math.

    The new project is created, and a blank form appears in the Designer.

  3. Click the Button control on the Windows Forms tab of the Toolbox, and create a button object at the top of your form.

  4. Click the TextBox control in the Toolbox, and draw a text box below the button object.

  5. Set the Text property of the button object to Square Root.

  6. Double-click the button object to display the Code Editor.

  7. At the very top of the Code Editor, above the Public Class Form1 statement, type the following program statement:

    Imports System.Math

    The Imports statement adds a library of objects, properties, and methods to your project. This statement must be the first statement in your program—it must come even before the variables that you declare for the form and the Public Class Form1 statement that Visual Basic automatically provides. The particular library you've chosen is the System.Math class, a collection of objects, properties, and methods provided by the .NET Framework for mathematical operations.

  8. Move down in the Code Editor, and add the following code to the Button1_Click event procedure between the Private Sub and End Sub statements:

    Dim Result As Double Result = Sqrt(625) TextBox1.Text = Result

    These three statements declare a variable of the double type named Result, use the Sqrt method to compute the square root of 625, and assign the Result variable to the Text property of the text box object so that the answer is displayed.

  9. Click the Save All button on the Standard toolbar to save your changes. Specify the c:\vb05sbs\chap05 folder as the location.

  10. Click the Start Debugging button on the Standard toolbar.

    The Framework Math program runs in the IDE.

  11. Click the Square Root button.

    Visual Basic calculates the square root of 625 and displays the result (25) in the text box. As you can see here, the Sqrt method works!

  12. Click the Close button on the form to end the program.

To use a particular .NET Framework class in your program, include the Imports statement, and specify the appropriate class library. You can use this technique to use any class in the .NET Framework, and you'll see many more examples of this technique as you work through Microsoft Visual Basic 2005 Step by Step.

Категории