Download presentation
Presentation is loading. Please wait.
Published byCharleen Sanders Modified over 8 years ago
1
Tutorial 3: Using Variables and Constants1 Tutorial 3 Using Variables and Constants
2
Tutorial 3: Using Variables and Constants2 Creating Variables and Named Constants Lesson A Objectives After completing this lesson, you will be able to: Create a local and form-level variable Select an appropriate data type for a variable Select an appropriate name for a variable Assign data to an existing variable Create a named constant
3
Tutorial 3: Using Variables and Constants3 Using Variables to Store Information The memory locations are called variables, because the contents of the locations can change as the program is running You should assign a descriptive name to each variable used in an application – it should help you remember the variable’s data type and purpose
4
Tutorial 3: Using Variables and Constants4 Selecting a Data Type for a Variable TypeSizeTypeSize Byte1Short2 Char2Integer4 Boolean4Long8 Decimal12Single4 Double8StringVaries Date8ObjectAnything
5
Tutorial 3: Using Variables and Constants5 Choose the Correct Data Type Short, Integer, LongUsed to store whole numbers Single, DoubleStore floating-point numbers DecimalStores numbers with a decimal point BooleanStores True and False CharStores one Unicode character ByteStores 8-bits of data DateStores date and time information StringStores a sequence of characters
6
Tutorial 3: Using Variables and Constants6 Selecting a Name for a Variable Figure 3-4 lists the three characters typically associated with the Visual Basic.NET data types It is a common practice to type the letter m and the three-character ID using lowercase letters, and then use Pascal-case for the remainder of the variable’s name
7
Tutorial 3: Using Variables and Constants7
8
8
9
9 Rules for Naming Variables Name must begin with a letter Name can contain only letters, numbers, and the underscore. No punctuation characters or spaces are allowed Name cannot exceed 255 characters Name cannot be a reserved word
10
Tutorial 3: Using Variables and Constants10 Declaring a Variable Accessor variablename As Datatype [ = InitialValue] Accessor is [Public|Private|Static|Dim] Dim intTotal As Integer Dim sngRadius As Single = 12 Dim intYellow, intBlue As Integer Public strName As String = “Diane Zak”
11
Tutorial 3: Using Variables and Constants11 Assigning Data to an Existing Variable Quotation marks differentiate a string from both a number and a variable name The Dim statement creates the intNumber variable in memory and automatically initializes it to the number 0
12
Tutorial 3: Using Variables and Constants12 Assigning Data to a Variable The intNumber = 500 assignment statement removes the zero from the intNumber variable and stores the number 500 there instead The intNumber = intNumber *2 assignment statement first multiplies the contents of the intNumber variable (500) by the number 2, giving 1000 The assignment statement is of the form variablename = value sngHours = 38.5 sngBonus = sngSales * 0.1 strName = “Mary” intNumber = 500
13
Tutorial 3: Using Variables and Constants13 The Scope of a Variable A variable’s scope indicates which procedures in an application can use the variable – either Dim, Public or Private statement is entered When you declare a variable in a procedure, the variable is called a local variable and is said to have procedure scope, because only that procedure can use the variable When you declare a variable in the form’s Declarations section, the variable is called a form- level variable and is said to have module scope
14
Tutorial 3: Using Variables and Constants14 Creating a Local Variable Created with the Dim statement 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
15
Tutorial 3: Using Variables and Constants15 Creating a Form-level Variable Created with the Public/Private statement Entered in a form’s General 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
16
Tutorial 3: Using Variables and Constants16 Constants Literal constant an item of data whose value cannot change while the program is running Examples: 7 “Mary” Named constant a memory location whose contents cannot be changed while the program is running Examples: conPi conRate
17
Tutorial 3: Using Variables and Constants17 Creating a Named Constant A memory location whose value cannot change during run time Syntax: [Public|Private] Const constname [As datatype] = expression Examples: Const conPi As Single = 3.141593 Public Const conMaxAge as Integer = 65
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.