Showing posts with label Programming Fundamentals. Show all posts
Showing posts with label Programming Fundamentals. Show all posts

Variables, Formulas, and the .NET Framework in Visual Basic 2010 Part 2

Formula is a statement that combines numbers, variables, operators, and keywords to create a new value. Visual Basic contains several language elements designed for use in formulas. In this section, you’ll practice working with arithmetic (or mathematical) operators, the symbols used to tie together the parts of a formula. With a few exceptions, the arithmetic symbols you’ll use are the ones you use in everyday life, and their operations are fairly intuitive. You’ll see each operator demonstrated in the following exercises.

Arithmetic Operators











Using Parentheses in a Formula
You can use one or more pairs of parentheses in a formula to clarify the order of precedence or impose your own order of precedence over the standard one. For example, Visual Basic calculates the formula

Number = (8 – 5 * 3) ^ 2
 
by determining the value within the parentheses (–7) before doing the exponentiation—even though exponentiation is higher in order of precedence than subtraction and multiplication, according to the preceding table. You can further refine the calculation by placing nested parentheses in the formula. For example,

Number = ((8 – 5) * 3) ^ 2

directs Visual Basic to calculate the difference in the inner set of parentheses first, perform the operation in the outer parentheses next, and then determine the exponentiation. The result produced by the two formulas is different: the first formula evaluates to 49 and the second to 81. Parentheses can change the result of a mathematical operation, as well as make it easier to read.

Variables, Formulas, and the .NET Framework in Visual Basic 2010


Variable
A variable is a temporary storage location for data in your program. You can use one or many variables in your code, and they can contain words, numbers, dates, properties, or other values. By using variables, you can assign a short and easy-to-remember name to each piece of data you plan to work with. Variables can hold information entered by the user at run time, the result of a specific calculation, or a piece of data you want to display on your form. In short, variables are handy containers that you can use to store and track almost any type of information.

Using variables in a Visual Basic program requires some planning. Before you can use a variable, you must set aside memory in the computer for the variable’s use. This process is a little like reserving a seat at a theater or a baseball game. I’ll cover the process of making reservations for, or declaring, a variable in the next section.

The Dim Statement
To declare a variable in Visual Basic 2010, type the variable name after the Dim statement. (Dim stands for dimension.) This declaration reserves room in memory for the variable when the program runs and lets Visual Basic know what type of data it should expect to see later. Although this declaration can be done at any place in the program code (as long as the declaration happens before the variable is used), most programmers declare variables in one place at the top of their event procedures or code modules.

For example, the following statement creates space for a variable named FirstName that will hold a textual, or string, value:

Dim FirstName as String

Use the As keyword to give the variable a particular type, and I’ve identified the type by using the keyword String. (You’ll learn about other data types later in this chapter.) A string variable contains textual information: words, letters, symbols—even numbers. I find myself using string variables a lot; they hold names, places, lines from a poem, the contents of a file, and many other “wordy” data.

Implicit Variable Declaration
If you really want to declare variables “the old way” in Visual Basic 2010—that is, without explicitly declaring them by using the Dim statement—you can place the Option Explicit Off statement at the very top of your form’s or module’s program code (before any event procedures), and it will turn off the Visual Basic default requirement that variables be declared before they’re used. This method is not recommend, but you might find it useful temporarily as you convert older Visual Basic programs to Visual Basic 2010.

Another possibility is to use the Option Infer statement, which was added to Visual Basic 2008. If Option Infer is set to On, Visual Basic will deduce or infer the type of a variable by examining the initial assignment you make. This allows you to declare variables without specifically identifying the type used, and allowing Visual Basic to make the determination. For example, the expression

Dim attendance = 100

will declare the variable named attendance as an Integer, because 100 is an integer expression. In other words, with Option Infer set to On, it is the same as typing

Dim attendance As Integer = 100

Likewise, the expression

Dim address = "Bireuen"

will declare the variable address as type String, because its initial assignment was of type String. If you set Option Infer to Off, however, Visual Basic will declare the variable as type Object—a general (though somewhat bulky and inefficient) container for any type of data.

If you plan to use Option Infer to allow this type of inferred variable declaration (a flexible approach, but one that could potentially lead to unexpected results), place the following two statements at the top of your code module (above the Class Form statement):

Option Explicit Off
Option Infer On

Option Explicit Off allows variables to be declared as they are used, and Option Infer On allows Visual Basic to determine the type automatically. You can also set these options using the Options command on the Tools menu, as discussed in Chapter 1, “Exploring the Visual Studio Integrated Development Environment.”
 

About the Author

My name is Jakfar Zakaria. My friends call me Jakfar. I have lived such a tiring journey, about my education and others. Four years ago, I explore the computer network specializes in Linux Technology. And now I steeped about computer programming. I have little ability in computer programming, I focus on Visual Basic .NET programming language and PHP programming language. It takes a big sacrifice in studying it all, and my teacher is Google! Yups, Google! Google is a good teacher for me.

JOIN THE COMMUNITY!

TAKE A BREAK