Monday, June 3, 2013

Visiual Basic tutorial


Visual Basic Data Type
Md. Xamil Hassan Toneyy

Understanding Constants

When you hard-code numbers in your procedures (such as in intVotingAge = 18), a myriad of things can go wrong.
Hard-coded numbers often are called magic numbers because they're usually shrouded in mystery. The meaning of
such a number is obscure because the digits themselves don't indicate what the number represents. Constants are used
to eliminate the problems of magic numbers.
You define a constant as having a specific value at design time, and that value never changes throughout the life of
your program. Constants offer the following benefits:
They eliminate or reduce data-entry problems: It's much easier to remember to use a constant named c_pi than
it is to enter 3.14159265358979 everywhere that pi is needed. The compiler catches misspelled or undeclared
constants, but it doesn't care one bit what you enter as a literal value.
Code is easier to update: If you hard-coded a mortgage interest rate at 6.785, and the rate changed to 7.00,
you'd have to change every occurrence of 6.785 in code. In addition to the possibility of data entry problems,
you'd run the risk of changing a value of 6.785 that had nothing to do with the interest rate perhaps a value that
represented a savings bond yield (okay, a very high savings bond yield). With a constant, you change the value
once at the constant declaration, and all code that references the constant uses the new value right away.
Code is easier to read: Magic numbers are often anything but intuitive. Well-named constants, on the other
hand, add clarity to code. For example, which of the following statements makes more sense to you?
decInterestAmount = CDec((decLoanAmount * 0.075) * 12)
or
decInterestAmount = CDec((decLoanAmount * c_sngInterestRate) * _
c_intMonthsInTerm)
Constant definitions have the following syntax:
Const name As datatype = value
To define a constant to hold the value of pi, for example, you could use a statement such as this:
Const c_pi As Single = 3.14159265358979
Note how I prefix the constant name with c_. I do this so that it's easier to determine what's a variable and what's a
constant when reading code. See the "Naming Conventions" section later in this chapter for more information.
After a constant is defined, you can use its name in code in place of its value. For example, to output the result of 2
times the value of pi, you could use a statement like this (the * character is used for multiplication):
Debug.WriteLine(c_pi * 2)
Using the constant is much easier and less prone to error than typing this:
Debug.WriteLine(3.14159265358979 * 2)
Constants can be referenced only in the scope in which they are defined. I discuss scope in the section "Determining
Scope" later in this chapter.
You'll use what you learn in this chapter to enable the options controls that you added in Chapter 6, "Working with
Controls." The first thing you'll do is use constants to create default values for the options. Recall from Chapter 6 that
you created an option form that allowed the user to manipulate the following three options:
The user's name: This is displayed in the Picture Viewer's main form title bar.
Prompt to confirm on exit: This is used to determine whether the user is asked if he or she really wants to shut
down the Picture Viewer application.
The default background color of the picture box: This can be set to gray (the default) or white.
In the following steps, you'll create a constant for the default value of the Prompt on Exit option. Start by opening the
Picture Viewer project from Chapter 8, and then follow these steps:
1. Click ViewerForm.vb in the Solution Explorer to select it.
2. Click the View Code button at the top of the Solution Explorer to view the code behind ViewerForm.vb.
3. The constants you are about to create will be module-level constants. That is, they can be used
anywhere within the module in which they are declared. This means that they won't be placed in a specific
procedure. The place to put module constants is right after the declaration of the module toward the top
(Public Class classname).
Position the cursor on the line following the declaration, press Enter to create a new line, and then enter
the following constant declaration:
Const c_defPromptOnExit = False
In the next section, you'll learn how to use this constant to set the value of a variable.

Row 2 Cell 2

No comments: