Chapter 3 Using Variables and Constants Programming with
Chapter 3: Using Variables and Constants Programming with Microsoft Visual Basic. NET, Second Edition
Using Variables to Store Information • Besides storing data in the properties of controls, a programmer also can store data, temporarily, in memory locations inside the computer • The memory locations are called variables, because the contents of the locations can change as the program is running Programming with Microsoft Visual Basic. NET, Second Edition 2
Using Variables to Store Information (continued) • One use for a variable is to hold information that is not stored in a control on the user interface • You can also store the data contained in a control’s property in a variable Programming with Microsoft Visual Basic. NET, Second Edition 3
Selecting a Data Type for a Variable Type Memory Required Byte 1 byte Short 2 bytes Char 2 bytes Integer 4 bytes Boolean 2 bytes Long 8 bytes Decimal 16 bytes Single 4 bytes Double 8 bytes String Varies Date 8 bytes Object 4 bytes Programming with Microsoft Visual Basic. NET, Second Edition 4
Selecting a Data Type for a Variable (continued) Short, Integer, Long Store whole numbers Single, Double Store floating-point numbers Decimal Stores numbers with a decimal point Boolean Stores True and False Char Stores one Unicode character Byte Stores 8 -bits of data Date Stores date and time information String Stores a sequence of characters Programming with Microsoft Visual Basic. NET, Second Edition 5
Selecting a Name for a Variable • The naming convention used in this book: – The name indicates only the variable’s purpose and is entered using lowercase letters – Use camel casing: if a variable’s name contains two or more words, you capitalize the first letter in the second and subsequent words • The name assigned to a variable must follow the rules listed in Figure 3 -4 Programming with Microsoft Visual Basic. NET, Second Edition 6
Selecting a Name for a Variable (continued) Figure 3 -4: Rules for variable names along with examples of valid and invalid names Programming with Microsoft Visual Basic. NET, Second Edition 7
Declaring a Variable • You use a declaration statement to declare, or create, a variable • Syntax: {Dim | Private | Static} variablename [As datatype][= initialvalue] • Examples: – Dim hours. Worked As Integer – Dim data. Ok As Boolean = True – Dim name As String, age As Integer Programming with Microsoft Visual Basic. NET, Second Edition 8
Assigning Data to an Existing Variable • You use an assignment statement to assign a value to a variable while an application is running • Syntax: variablename = value • Examples: – quantity. Ordered = 500 – first. Name = “Mary” – state = Me. ui. State. Text. Box. Text – discount. Rate =. 03 Programming with Microsoft Visual Basic. NET, Second Edition 9
Assigning Data to an Existing Variable (continued) • A literal constant is an item whose value does not change while the application is running • String literal constants are enclosed in quotation marks, but numeric literal constants and variable names are not • A literal type character forces a literal constant to assume a data type other than the one its form indicates Programming with Microsoft Visual Basic. NET, Second Edition 10
Assigning Data to an Existing Variable (continued) • A variable can store only one item of data at any one time • When you use an assignment statement to assign another item to the variable, the new data replaces the existing data • After data is stored in a variable, you can use the data in calculations Programming with Microsoft Visual Basic. NET, Second Edition 11
The Parse Method • Every numeric data type in Visual Basic. NET has a Parse method that can be used to convert a string to that numeric data type • Syntax: numeric. Data. Type. Parse(string) • Example: Dim sales As Decimal sales = Decimal. Parse(Me. ui. Sales. Text. Box. Text) Programming with Microsoft Visual Basic. NET, Second Edition 12
The Convert Class • The Convert class contains methods to convert a numeric value to a specified data type • Syntax: Convert. method(value) • Example: Dim purchase As Double = 500 Dim tax As Decimal tax = Convert. To. Decimal(purchase) *. 03 D Programming with Microsoft Visual Basic. NET, Second Edition 13
The Convert Class (continued) Figure 3 -9: Most commonly used methods contained in the Convert class Programming with Microsoft Visual Basic. NET, Second Edition 14
The Scope and Lifetime of a Variable • A variable’s scope indicates which procedures in an application can use the variable • The scope is determined by where the Dim, Public or Private statement is entered • When you declare a variable in a procedure, the variable is called a procedure-level variable and is said to have procedure scope Programming with Microsoft Visual Basic. NET, Second Edition 15
The Scope and Lifetime of a Variable (continued) • When you declare a variable in the form’s Declarations section, it is called a module-level variable and is said to have module scope • Block-level variables are declared within specific blocks of code, such as within If. . . Then. . . Else statements or For. . . Next statements Programming with Microsoft Visual Basic. NET, Second Edition 16
The Scope and Lifetime of a Variable (continued) • Creating a procedure-level variable – Created with the Dim keyword – The Dim statement is entered in an object’s event procedure – Only the procedure in which it is declared can use the variable – Removed from memory when the procedure ends Programming with Microsoft Visual Basic. NET, Second Edition 17
The Scope and Lifetime of a Variable (continued) • Creating a module-level variable – Created with the Private keyword – Entered in a form’s Declarations section – Can be used by any of the procedures in the form – Removed from memory when the application ends or the form is destroyed Programming with Microsoft Visual Basic. NET, Second Edition 18
Named Constants • A memory location whose contents cannot be changed while the program is running • You create a named constant using the Const statement • Syntax: Const constantname [As datatype] = expression • Example: – Const PI As Double = 3. 141593 Programming with Microsoft Visual Basic. NET, Second Edition 19
Concatenating Strings • Connecting strings together is called concatenating • Use the concatenation operator, which is the ampersand (&), to concatenate strings in Visual Basic. NET • When concatenating strings, be sure to include a space before and after the concatenation operator Programming with Microsoft Visual Basic. NET, Second Edition 20
Concatenating Strings (continued) Example Result first. Name & last. Name Sue. Chen first. Name & “ “ & last. Name Sue Chen last. Name & “, “ & first. Name Chen, Sue “She is “ & Convert. To. String(age) & “!” She is 21! “She is “ & age & “!” She is 21! Programming with Microsoft Visual Basic. NET, Second Edition 21
The Input. Box Function • The Input. Box function displays one of Visual Basic. NET’s predefined dialog boxes • Syntax: Input. Box(prompt[, title][, default. Response]) • Use sentence capitalization for the prompt, and book title capitalization for the title • Has limitations: can’t control appearance and allows user to enter only one piece of data Programming with Microsoft Visual Basic. NET, Second Edition 22
The Input. Box Function (continued) Figure 3 -29: Example of a dialog box created by the Input. Box function Programming with Microsoft Visual Basic. NET, Second Edition 23
The New. Line Character • The New. Line character, which is Chr(13) & Chr(10), instructs the computer to issue a carriage return followed by a line feed • The Control. Chars. New. Line constant advances the insertion point to the next line on the screen • The Control. Chars. New. Line constant is an intrinsic constant, which is a named constant that is built into Visual Basic. NET Programming with Microsoft Visual Basic. NET, Second Edition 24
Designating a Default Button • Can be selected by pressing the Enter key even when the button does not have the focus • Set the form’s Accept. Button property to the desired button • If used, it is typically the first button • If a button’s action is destructive and irreversible, then it should not be the default button Programming with Microsoft Visual Basic. NET, Second Edition 25
Static Variables • A static variable is a local variable that retains its value when the procedure in which it is declared ends • Syntax: Static variablename [As datatype] [= initialvalue] • Removed from memory when application ends or form is removed from memory Programming with Microsoft Visual Basic. NET, Second Edition 26
- Slides: 26