Presentation is loading. Please wait.

Presentation is loading. Please wait.

Slide 1 Variables, Constants and Data Types. Slide 2 Variables v Three components define a variable: –Name (memory location) –Type –Information (value)

Similar presentations


Presentation on theme: "Slide 1 Variables, Constants and Data Types. Slide 2 Variables v Three components define a variable: –Name (memory location) –Type –Information (value)"— Presentation transcript:

1 Slide 1 Variables, Constants and Data Types

2 Slide 2 Variables v Three components define a variable: –Name (memory location) –Type –Information (value) v Variable name must: –Start with a letter –No spaces, periods nor punctuation characters –Unique (within its scope) –255 characters in length –Not a VB reserved word

3 Slide 3 Variables v Variable naming conventions: –Descriptive (code is easy to read) –Short as possible (easy to type) –Use a prefix for type (easy for programmer to know its type) u String ssName u IntegernnAge u (table 8.1, page 161)

4 Slide 4 Types of Variables v Integer2 bytes-32768 to +32767 v Long4+/-2 billion v Single4+/-1E-45 to 4E38 v Double8+/-5E-324 to 1.8E308 v Currency8+/- 9E14 v String1B/char65000 fixed, 2 billion dynamic v Byte10 to 255 v Boolean2True or False v Date81/1/100 to 12/31/999 v Object4 v Varient16B+1B/Char

5 Slide 5 Variable Declaration v If a variable is not declared, it is declared by default by VB as Variant –Waste memory resources –Type might be invalid for use with functions v It is good programming practice to declare variables

6 Slide 6 Explicit Declaration v Explicit declaration uses statements to define the names and types of variables –Dim sFname as String, sLname as String –Private sFname as String, sLname as String –Static sFname as String, sLname as String –Public sFname as String, sLname as String v Multiple variables with multiple types v If type is not indicated, Variant will be used

7 Slide 7 Implicit Declaration v VB defines the variable the first time it is used v Variable name is preceded with a special character: –Integer% –Long& –Single! –Double# –Currency@ –String$ v Example: – nNumVal% = 0 –sFirstName$ = “Ali”

8 Slide 8 Fixed-Length Strings v Strings in general are variable-length v Fixed length strings are declared like: –Dim sName As String * 25 v If a shorter string is assigned, it is appended with blanks v If a longer string is assigned, it is truncated

9 Slide 9 Variable Array v Array: is a group of variables of the same type, sharing the same name v Example: Dim nScores (1 to 35) As Integer v Use for loop with array For nCounter = 1 to 20 nScores(nCounter) = 0 Next nCounter

10 Slide 10 Scope v By default variables are local to the procedures where they are created in (local variable) v Public variables: can be accessed anywhere at the code v Example: Public sUserName As String (Defined in the General Declarations Section) v Using Public variables: –Hard to debug –Bad use of resources

11 Slide 11 Static Variables v Variables declared local to a procedure are discarded afterwards v To preserve a variable in side a procedure use Static v Static nPages As Integer v Using Static with functions or sub treats all variables inside as Static

12 Slide 12 Option Explicit v Setting “REqire Variable Declaration” option (Tools – Options – Editor) v It places Option Explicit in the general section v has no effect on Forms/Modules created before setting it

13 Slide 13 Constants v Cannot be modified v Easy to remember then it value v Avoid typing long strings v Minimize program modifications v Examples: –Const vbActibeTitleBar = -2147483646 –Const PI = 3.141592654 –Public Const PI = 3.141592654

14 Slide 14 Converting Data Types v VB provides several conversion functions you can use to convert values into a specific data type. v To convert a value to Currency, for example, you use the CCur function: PayPerWeek = CCur(hours * hourlyPay)

15 Slide 15 The Empty Value v Sometimes you need to know if a value has ever been assigned to a created variable. A Variant variable has the Empty value before it is assigned a value. The Empty value is a special value different from 0, a zero-length string (""), or the Null value. You can test for the Empty value with the IsEmpty function: –If IsEmpty(Z) Then Z = 0 v When a Variant contains the Empty value, you can use it in expressions; it is treated as either 0 or a zero-length string, depending on the expression. v The Empty value disappears as soon as any value (including 0, a zero-length string, or Null) is assigned to a Variant variable. You can set a Variant variable back to Empty by assigning the keyword Empty to the Variant.

16 Slide 16 The Null Value v The Variant data type can contain another special value: Null. v Null is commonly used in database applications to indicate unknown or missing data. Because of the way it is used in databases, Null has some unique characteristics: v Expressions involving Null always result in Null. Thus, Null is said to "propagate" through expressions; if any part of the expression evaluates to Null, the entire expression evaluates to Null. v Passing Null, a Variant containing Null, or an expression that evaluates to Null as an argument to most functions causes the function to return Null.

17 Slide 17 The Null Value v You can also assign Null with the Null keyword: –Z = Null v You can use the IsNull function to test if a Variant variable contains Null: If IsNull(X) And IsNull(Y) Then Z = Null Else Z = 0 End If

18 Slide 18 Built-in Constants v Intrinsic Constants v Colors, keycodes, shapes v Described in help v Object Browser


Download ppt "Slide 1 Variables, Constants and Data Types. Slide 2 Variables v Three components define a variable: –Name (memory location) –Type –Information (value)"

Similar presentations


Ads by Google