is437: spring 2006 instructor: dr. boris jukic
DESCRIPTION
IS437: Spring 2006 Instructor: Dr. Boris Jukic. Data Types, Constants, Variables, Scope, Conversion. Learning Objectives. data types - variables and constants - scope & lifetime - declaration (option explicit, option strict) assignment - numeric - string - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: IS437: Spring 2006 Instructor: Dr. Boris Jukic](https://reader036.vdocument.in/reader036/viewer/2022062305/568151be550346895dbff122/html5/thumbnails/1.jpg)
IS437: Spring 2006Instructor: Dr. Boris Jukic
Data Types, Constants, Variables, Scope, Conversion
![Page 2: IS437: Spring 2006 Instructor: Dr. Boris Jukic](https://reader036.vdocument.in/reader036/viewer/2022062305/568151be550346895dbff122/html5/thumbnails/2.jpg)
Learning Objectives
• data types - variables and constants - scope & lifetime - declaration (option explicit, option strict)• assignment - numeric - string - date• some useful functions & controls - type conversion
![Page 3: IS437: Spring 2006 Instructor: Dr. Boris Jukic](https://reader036.vdocument.in/reader036/viewer/2022062305/568151be550346895dbff122/html5/thumbnails/3.jpg)
Frequently-used data types• integer short 2 bytes -32,768 to +32,767 integer 4 bytes about +/- 2 billion long 8 bytes a very large integer number• real decimal 16 bytes 0 to 28 digits of accuracy single 4 bytes 38 to 45 digits of accuracy double 8 bytes 308 to 324 digits of accuracy• Boolean 2 bytes true or false• string 2 bytes for each character up to 2 billion Unicode
characters• date 8 bytes• object 4 bytes
![Page 4: IS437: Spring 2006 Instructor: Dr. Boris Jukic](https://reader036.vdocument.in/reader036/viewer/2022062305/568151be550346895dbff122/html5/thumbnails/4.jpg)
Constants
• Named constants: (programmer defined) e.g., const pi as single = 3.1415
const taxRate as single =0.07
• Intrinsic constants: (system provided) e.g., Color.White Color.Blue Color.Red Color.Yellow Color.Green ...
![Page 5: IS437: Spring 2006 Instructor: Dr. Boris Jukic](https://reader036.vdocument.in/reader036/viewer/2022062305/568151be550346895dbff122/html5/thumbnails/5.jpg)
Structure of a VB Application
Project (s)
Form(s)
Controls
solution
![Page 6: IS437: Spring 2006 Instructor: Dr. Boris Jukic](https://reader036.vdocument.in/reader036/viewer/2022062305/568151be550346895dbff122/html5/thumbnails/6.jpg)
Scope of variables & constants
dim i as integerconst m as short = 8
module-level (form): variables declared using the Private declarationprivate y as integerprivate const j as integer = 5
local (sub): variables declared using the Dim declaration
project level variables:variables declared using the Public declarationPublic z as integerPublic const k as integer = 15
block (within a loop)For i = 0 to 3 dim k as integer …Next i
![Page 7: IS437: Spring 2006 Instructor: Dr. Boris Jukic](https://reader036.vdocument.in/reader036/viewer/2022062305/568151be550346895dbff122/html5/thumbnails/7.jpg)
Lifetime of Variables
-local: only alive during the execution of a procedure or a block of code
-For temporary results/values
- form-level: alive as long as the form remains loaded (typically the whole lifetime of a project)
-(namespace) project : project
-If values are to be preserved after the project itself is not active
-External data depositories
![Page 8: IS437: Spring 2006 Instructor: Dr. Boris Jukic](https://reader036.vdocument.in/reader036/viewer/2022062305/568151be550346895dbff122/html5/thumbnails/8.jpg)
Declaration of Variables & Constants
• “option explicit on” as default to enforce declaration
• Why is declaration a good idea? - e.g., dim monument as integer rebates = 345678 …. rebtaes = rebates + 75
‘the error will be detected if declaration is mandatory ….• local declaration - e.g., Private Sub command1_click(…) … dim x as integer dim y const MARKUP as single = 0.14 … End sub
![Page 9: IS437: Spring 2006 Instructor: Dr. Boris Jukic](https://reader036.vdocument.in/reader036/viewer/2022062305/568151be550346895dbff122/html5/thumbnails/9.jpg)
Declaration of Variables & Constants
• form-level declaration
where? In a declaration section of the form private x, y, z as integerprivate const rstmsg as string = “Please Reset all Values”
• project-level declaration where? It is good practice to declare them all in a single module. (show in class)public x as integer
public const COMP_NAME as string = “First Trust and Bank”
public const MAX_BONUS as decimal = 11500
![Page 10: IS437: Spring 2006 Instructor: Dr. Boris Jukic](https://reader036.vdocument.in/reader036/viewer/2022062305/568151be550346895dbff122/html5/thumbnails/10.jpg)
Variable Declaration
![Page 11: IS437: Spring 2006 Instructor: Dr. Boris Jukic](https://reader036.vdocument.in/reader036/viewer/2022062305/568151be550346895dbff122/html5/thumbnails/11.jpg)
Project-level Declaration
![Page 12: IS437: Spring 2006 Instructor: Dr. Boris Jukic](https://reader036.vdocument.in/reader036/viewer/2022062305/568151be550346895dbff122/html5/thumbnails/12.jpg)
Structure of a VB Application
Project (s)
Form(s)
Controls
code module - declare public var. & constants- utility procedures such as sort, etc.
solution
![Page 13: IS437: Spring 2006 Instructor: Dr. Boris Jukic](https://reader036.vdocument.in/reader036/viewer/2022062305/568151be550346895dbff122/html5/thumbnails/13.jpg)
Choosing the variable scope
•Choose the variable and constant scope as narrow as possible
•minimizes computational needs of the code•Makes code more modular, divided into useful, independent components
![Page 14: IS437: Spring 2006 Instructor: Dr. Boris Jukic](https://reader036.vdocument.in/reader036/viewer/2022062305/568151be550346895dbff122/html5/thumbnails/14.jpg)
Recommended Naming Conventions
prefix• integer integer int long lng• real decimal dec single sng double dbl• Boolean bln• string str• date dat
Scope Prefix
project g
module (form) m
local none
For example:
gstrCustomerName
![Page 15: IS437: Spring 2006 Instructor: Dr. Boris Jukic](https://reader036.vdocument.in/reader036/viewer/2022062305/568151be550346895dbff122/html5/thumbnails/15.jpg)
Assignment (numeric)• operators
+ addition - subtraction * multiplication / division ^ exponentiation \ integer division 5 \ 2 = 2 mod modulus 5 mod 2 = 1
• e.g., a = (5 * 4) / 2
![Page 16: IS437: Spring 2006 Instructor: Dr. Boris Jukic](https://reader036.vdocument.in/reader036/viewer/2022062305/568151be550346895dbff122/html5/thumbnails/16.jpg)
Assignment (string variable)• empty string - e.g., dim x as string x = ““ text1.text = x
• assign one string to another - e.g., text2.text = text1.text
• string concatenation using “&” - e.g., dim firstname, lastname as string firstname = “John” lastname = “Smith” label1.text = firstname & “ “ & lastname• concatenate with itself - e.g., dim name as string name = “Roberts” name = name & “ Rogers”
John Smith
![Page 17: IS437: Spring 2006 Instructor: Dr. Boris Jukic](https://reader036.vdocument.in/reader036/viewer/2022062305/568151be550346895dbff122/html5/thumbnails/17.jpg)
Assignment (Date)
• e.g., dim date1, date2 as Date
date2 = #3-6-98 2:15#
date2 = #March 6, 1998 2:15 pm#
date2 = #14:15# date1 = “1/20/97” with option strict off
date1 = “2:12 am” with option strict off
![Page 18: IS437: Spring 2006 Instructor: Dr. Boris Jukic](https://reader036.vdocument.in/reader036/viewer/2022062305/568151be550346895dbff122/html5/thumbnails/18.jpg)
Option Strict
“Option Strict Off” is the default setting. Change the setting by typing “Option Strict On” at
the top of the code window. With “Option Strict On”, the system strictly enforces
data type checking. Data conversion from “wider” to “narrower” data type is not
allowed Conversion between string and numeric types is not
allowed This prevent many hard to find bugs and execution errors
![Page 19: IS437: Spring 2006 Instructor: Dr. Boris Jukic](https://reader036.vdocument.in/reader036/viewer/2022062305/568151be550346895dbff122/html5/thumbnails/19.jpg)
Option Strict
With “Option Strict Off”, it is okay to write: Dim s as string, i as single
s = “34”
i = s / 2
• With “Option Strict On”, the code above will generate an error message:
Option Strict On disallows implicit conversion from string to single.
![Page 20: IS437: Spring 2006 Instructor: Dr. Boris Jukic](https://reader036.vdocument.in/reader036/viewer/2022062305/568151be550346895dbff122/html5/thumbnails/20.jpg)
Widening & narrowingOption Strict Off
Option Strict On
Widening OK OK
Narrowing OK, but may result in over-flow exception in runtime
No
Widening: store a variable value into another variable with greater precision or magnitude
Narrowing: the reverse
Dim i as integer
Dim j as long
i = 35
j = i
Widening
![Page 21: IS437: Spring 2006 Instructor: Dr. Boris Jukic](https://reader036.vdocument.in/reader036/viewer/2022062305/568151be550346895dbff122/html5/thumbnails/21.jpg)
Narrowing (Option Strict Off)
Dim i As Integer ‘4-bytesDim j As Long ‘8-bytesj = 3000000000i = j
The above code will generate a runtime exception, but not a warning in design time.
![Page 22: IS437: Spring 2006 Instructor: Dr. Boris Jukic](https://reader036.vdocument.in/reader036/viewer/2022062305/568151be550346895dbff122/html5/thumbnails/22.jpg)
Data type conversion functions
CStr string CInt integer CLng long CSng single CDbl double CDec decimal CDate date …
Convert its argument toFunction Dim x as integer
Dim s as string
x = 5
s = CStr(x)
s = “5”
Dim d as date
Dim s as string
s = “01/15/2003”
d = CDate(s)
d = #01/15/2002#