Title: Visual Basic .Net the Programming Language
1Visual Basic .Net the Programming Language
Syntax and semantics
2structure of computer languages
- data types (variables constants)
- assignment
- flow control
- - branching
- - looping
- input/output
3Learning Objectives
- data types
- - variables and constants
- - scope lifetime
- - declaration (option explicit, option
strict) - assignment
- - numeric
- - string
- - date
- some useful functions controls
- - type conversion
- - date related (DateTimePicker)
- - numeric updown controls
-
4Frequently-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
5Constants
- Named constants (programmer defined)
- e.g., const pie as single 3.1415
- Intrinsic constants (system provided)
- e.g., Color.White
- Color.Blue
- Color.Red
- Color.Yellow
- Color.Green
- ...
-
6Structure of a VB Application
solution
Project (s)
A project embodies an application program.
Form(s)
Controls
7Scope of variables constants
project Public (or friend) z as integer Public
(or friend) const k as integer 15
module (form) private y as integer private const
j as integer 5
local (sub)
dim i as integer const m as short 8
block (within a loop) For i 0 to 3 dim k as
integer Next i
8Lifetime of Variables
- local, block procedure or block of code -
form-level when the form remains loaded -
project project
9Declaration of Variables Constants
- option explicit on as default to enforce
- declaration
- Why is declaration a good idea?
- - e.g., dim monument as integer
- monument 345678
- .
- monumnet monument 75
- .
- local declaration
- - e.g., Private Sub command1_click()
- dim x as integer
- dim y
- const pie as single
3.1415 -
- End sub
10Declaration of Variables Constants
- form-level declaration
- where? Before the first procedure of a
form - e.g., private x, y, z as integer
- private const country as
string The United States - project-level declaration
- where? in a module
- e.g., public x as integer
- public const country The
United States
11Variable Declaration
12Project-level Declaration
13Structure of a VB Application
solution
14Choose scope for variables
choose a scope as narrow as possible
15Naming 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
16Assignment (numeric)
- operators
- addition
- - subtraction
- multiplication
- / division
- exponentiation
- \ integer division
5 \ 2 2 - mod modulus
5 mod 2 1 - e.g., a (5 4) / 2
17Assignment (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
18Assignment (Date)
- e.g., dim date1, date2 as Date
- date2 3-6-98 215
- date2 March 6, 1998 215 pm
- date2 1415
- date1 1/20/97 with option
strict off - date1 212 am with
option strict off
19Option 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.
20Option 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.
21Widening narrowing
Dim i as integer Dim j as long i 35 j i
Widening store a variable value into another
variable with greater precision or
magnitude Narrowing the reverse
widening
22Narrowing (Option Strict Off)
Dim i As Integer 4-bytes Dim j As Long
8-bytes j 3000000000 i j
The above code will generate a runtime exception,
but not a warning in design time.
23data type conversion functions
Convert its argument to
Function
Dim x as integer Dim s as string x 5 s CStr(x)
CStr string CInt
integer CLng
long CSng single CDbl
double CDec
decimal CDate date
s 5
Dim d as date Dim s as string s 01/15/2003 d
CDate(s)
d 01/15/2002
24DateTimePicker Control
- Important properties
- Format long, short, time
- Value date time selected or set
- MinDate, MaxDate
Label1.Text CStr(Month(DateTimePicker1.Value))
25Date related Properties functions
- Now
- Today
- TimeofDay
- Year()
- Month()
- Day()
- Hour()
- Minute()
- Second()
26The DateDiff function
datediff(interval, date1, date2) where interval
DateInterval.Day
DateInterval.Month
DateInterval.Hour e.g.,
datediff (DateInterval.Day, 1/16/97,
1/1/2006) 2541
27Numeric UpDown Controls
28Numeric UpDown Controls
- Important properties
- Minimum, Maximum
- Increment
- DecimalPlaces
- Value
29 Private Sub ComputeButton_Click() Handles
ComputeButton.Click Dim rate, period,
principal As Double Dim monthlypayment As
Double rate CDbl(UpDown1.Value / 12)
'convert to monthly rate period
CDbl(UpDown3.Value) principal
CDbl(UpDown2.Value 1000)
monthlypayment Pmt(rate, period, principal)
-1 lblResult.Text FormatCurrency(monthly
payment.ToString) lblResult.Visible
True End Sub
30 Private Sub ClearButton_Click() Handles
ClearButton.Click UpDown1.Value 0
UpDown2.Value 0 UpDown3.Value 0
lblResult.Visible False End Sub