Download presentation
Presentation is loading. Please wait.
Published byLinette Jackson Modified over 8 years ago
1
CIS 338: VB Variables Dr. Ralph D. Westfall April, 2011
2
Longest Book? How many words are there in the longest book? How many characters are there in a word, on average (including the space following it)? How many copies of the longest book could go into one VB character variable?
3
Variables a variable has the following: name (looks up its location in memory) must start with a letter (not a number) data type e.g., various types of numbers, character, boolean, etc. value (stored in the memory location) the value can change (it can vary) "variable cloudiness"
4
Variable Naming Issues advantages of a convention e.g. txtAge makes code easier to debug and maintain? naming objects makes it easier for Prof. Westfall to grade projects (this is a hint!) disadvantages of a convention extra work (at 1 st, but less debugging later?)
5
Integer VB.NET Variable TypesVB.NET Variable Types Byte – unsigned integer: 0 to 255 Char – unsigned integer: 0 to 65,535 (Unicode) Short – signed, same as Int16 –32768 to +32767 Integer – signed, same as Int32 –2 billion to + 2 billion Long Int64 – signed, same as Int64 –9 quintillion to +9 quintillion need to know that these data types are whole numbers (integers means no decimal points)
6
Decimal VB.NET Variable TypesVB.NET Variable Types Single: 4 bytes, 6-digit accuracy Double: 8 bytes, 14-digit accuracy can have 300+ zeros before/after decimal Decimal: 12 bytes, 28-digit accuracy (fixed decimal point, data never rounds) can have <= 29 digits before decimal point or 28 zeros/digits after the decimal point know which data types are decimals see notes
7
Other VB.NET Variable Types String: "Lee" 2 billion character maximum string size! ? Char – Unicode character (2 bytes) integer value that identifies character Boolean: True or False (4 bytes!) Date: 1 – 9999 AD (or CE) time and date stored in a variable, can extract or display the part(s) you want Variants do not exist in.NET can use Objects instead for whatever 'code can use Objects instead
8
Variable Naming Conventions in some programming languages, it is common to prefix the data type (1 - 3 chars) to the front of the variable name also known as "Hungarian notation""Hungarian notation" used to be popular at Microsoft not popular at all with Java programmers cultural issue between Microsoft and rest of world? for objects, Prof. Westfall still likes it
9
Naming Class Variables formerly used naming convention for member (class or module) variables was to put an m (for module) in front of the other prefix e.g., mals (or m_als) for an arraylist most of recent sources recommend against this
10
Declaring Variables Explicitly Dim vname [as vartype] [,vname2 ] as vartype vartype is required if Option Strict is On (otherwise defaults based on the values assigned to the variables) can declare multiple variables with one Dim but Prof. W really dislikes it ( hint) Dim nI, nJ, nK as Integer ' * Dim nI as Integer, nK as Integer ' Dim nI as Integer, dJ as Double ' '* 1-letter naming convention
11
Declaring String Length could declare a String as a specified length in VB 6 Dim sName as String * 20 unused space gets filled with spaces sName ="Joe" becomes "Joe " length is actual String length in.NET.PadRight() function adds trailing spaces "Joe".PadRight(20) 'to get 20 characters
12
Declaring Variables Implicitly can declare without types if Option Strict is Off (not On) 'but let's NOT do this nNumber = 0 'default based on type/size can also declare with letters or special characters (but this is kind of confusing) decCash = 1234.00D (Decimal) 'need to use this in decimal calculations lngPop& =1000 or lngPop = 1000L (Long ) dblRate#=.05 (Double) strChar$ = "Z" (String) etc.
13
Constants value is "hard coded" program can't change the value Const TAX_BASE_RATE =.15 Visual Basic "intrinsic constants" numeric values – make something happen easier to remember than numbers use vbOKOnly for message box style actually vbOKOnly = 0
14
Variable Scope - Public Public varname as vartype Public replaces Dim can't declare as Public inside a sub can be used by code outside this file (has namespace scope) disadvantages uses more memory can make code harder to maintain may be hard to locate its declaration
15
Variable Scope - Module Dim varname [as vartype] in general declarations (above/outside of procedures) can only be used by code in this file or class/module advantage: easier to maintain can use Private instead of Dim outside of procedures same effect
16
Variable Scope - Procedure Dim varname [as vartype] can declare with Dim, but not Public inside a procedure (Sub or Function) accessible only in the procedure itself, including in its blocks (next slide) value is lost when procedure terminates
17
Variable Scope - Block declared within control structures selection, looping, try/catch only accessible inside the block value is lost when execution exits the block if want to use it later, declare it outside of the block (e.g., outside of a loop)
18
Variable Scope - Static Static varname [as vartype] varname doesn't lose its value when its procedure terminates in a program example: in a print subroutine, a static variable can save the page number adds 1 to the static value when procedure prints another page in current report value stored while other procedures run
19
Procedures Have Scope Too Public Function MyFunction Private Sub MySub public Subs can be run by other files without Private, a Sub defaults to Public variables in a procedure must be declared with Dim (are private) a Private procedure can still set variables in general declarations (outside of itself) 'notes
20
Option Explicit On requires you to declare all variables helps spot spelling errors during development removing Option Explicit might improve efficiency when project goes into production can set in Visual Studio for all future projects Tools>Options>Projects and Solutions>VB Defaults>Option Explicit>On or can type at top of a previously created code file: Option Explicit On
21
Option Strict On automatically turns Option Explicit on forces you to also include the data type in a declaration also have to use conversion functions (e.g., CInt, CStr, etc.) when assigning a value to a variable of a different type can set in VS for all future projects Tools>Options>Projects and Solutions>VB Defaults>Option Explicit>On
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.