Part II: Programming Fundamentals

Part II
Programming Fundamentals

Chapter 5

Visual Basic Variables and Formulas, and the .NET Framework

After completing this chapter, you will be able to:

  • Use variables to store data in your programs.

  • Get input by using the InputBox function.

  • Display messages by using the MsgBox function.

  • Work with different data types.

  • Use variables and operators to manipulate data.

  • Use methods in the .NET Framework.

  • Use mathematical operators and functions in formulas.

In Part I, you learned how to create the user interface of a Microsoft Visual Basic 2005 program and how to build and run a program in the Microsoft Visual Studio 2005 development environment. In the nine chapters in Part II, you'll learn more about Visual Basic program code—the statements and keywords that form the core of a Visual Basic program. You'll learn how to manage information within programs and control how your code is executed, and you'll learn how to use decision structures, loops, timers, arrays, collections, and text files. You'll also learn how to debug your programs and handle run-time errors if they occur. After you complete Part II, you'll be ready for more advanced topics, such as customizing the user interface, database programming, and Web programming.

In this chapter, you'll learn how to use variables and constants to store data temporarily in your program, and how to use the InputBox and MsgBox functions to gather and present information by using dialog boxes. You'll also learn how to use functions and formulas to perform calculations, and how to use mathematical operators to perform tasks such as multiplication and string concatenation. Finally, you'll learn how to tap into the powerful classes and methods of the Microsoft .NET 2.0 Framework to perform mathematical calculations and other useful work.

Upgrade Notes: Migrating Visual Basic 6 Code to Visual Basic 2005

If you're experienced with Microsoft Visual Basic 6, you'll notice some new features in Visual Basic 2005, including the following:

  • To encourage better programming practices and cleaner program code, all Visual Basic 2005 variables must be declared before they're used. The implicit declaration of variables (using variables without declaring them) is allowed only if you use the Option Explicit Off statement—a practice that's discouraged.

  • Visual Basic no longer supports the Variant data type. You should declare all variables by using Dim and the keyword As to identify the type of data that they'll hold.

  • There are several new fundamental data types, and some of the older data types now support different ranges. For example, there's a 16-bit Short data type, a 32-bit Integer data type, and a 64-bit Long data type. The Visual Basic 6 Currency data type has been replaced with the Decimal data type.

  • Beginning with Microsoft Visual Studio .NET 2002, Visual Basic offered a pro-gram statement and compiler setting named Option Strict, which (when enabled) required that variables be of the same data type when they are added, compared, or combined. In Visual Basic 2005, this compiler setting is turned off by default, meaning that variables of different data types can be combined under certain circumstances without generating a compiler error. (To check your compiler setting, click Options on the Tools menu, click Projects And Solutions, and then click VB Defaults.) The designers of Visual Studio offer this setting as a “professional grade” protection against unwanted type-mismatch errors, and it can be a useful safety measure in larger programming projects. (If you enable Option Strict, you'll need to become familiar with data type conversion functions such as CInt, CLng, and CType to make different types of data compatible.) However, in this book I assume that you have Option Strict set to False, and in a few cases the sample programs will not run correctly if you have Option Strict set to True.

  • Visual Basic 2005 offers shortcut symbols for certain basic mathematical operations, such as addition (+), subtraction (-), and multiplication (*). With these shortcuts, you can write a formula such as X = X + 2 by using the syntax X += 2.

  • Visual Basic 2005 no longer provides built-in keywords, such as Abs or Cos, for mathematical operations. Instead, you must use the methods in the System.Math class library of the .NET Framework for mathematical functions. The functionality of these methods is similar to the familiar Visual Basic 6 functions, although a few names have changed (for example, Sqr is now Sqrt).

  • Visual Studio includes a MessageBox object, which is an alternative to the MsgBox function for displaying message boxes. To display a message box, you use the MessageBox.Show method.