Beruflich Dokumente
Kultur Dokumente
Contents
Objectives
At the end of this module, you should be able to:
Agenda
Fundamentals
Operators
Flow Controls
'
'
'
'
2. Namespace declaration
Namespace VBNETSchool
Public Class VBNetOne
''' <summary>
''' </summary>
''' <param name="args"></param>
Public Shared Sub Main(ByVal args As
String())
' print a message
A.WriteLine("Welcome to VB.NET!")
End Sub
End Class
Imports System
Imports A = System.Console
End Namespace
' insert
comments here
2. Documentation Comment
''' <summary>
''' </summary>
''' <param
name="args"></param>
Whitespaces
Tabs and spaces are ignored by
the compiler. They are used to
improve the readability of code.
'
'
'
'
Imports System
Imports A = System.Console
Namespace VBNETSchool
Public Class VBNetOne
''' <summary>
''' </summary>
''' <param name="args"></param>
Public Shared Sub Main(ByVal args As
String())
' print a message
A.WriteLine("Welcome to VB.NET!")
End Sub
End Class
End Namespace
'
'
'
'
Imports System
Imports A = System.Console
Namespace VBNETSchool
Public Class VBNetOne
''' <summary>
''' </summary>
''' <param name="args"></param>
Public Shared Sub Main(ByVal args As String())
' print a message
A.WriteLine("Welcome to VB.NET!")
End Sub
End Class
End Namespace
Main() method
This line begins the Main()
method. This is the line at which
the program will begin executing.
ByVal args As
String()
Declares a parameter named
args, which is an array of string.
It represents command-line
arguments.
'
'
'
'
Imports System
Imports A = System.Console
Namespace VBNETSchool
Public Class VBNetOne
''' <summary>
''' </summary>
''' <param name="args"></param>
Public Shared Sub Main(ByVal args As String())
' print a message
A.WriteLine("Welcome to VB.NET!")
End Sub
End Class
End Namespace
VB.NET
statement
A complete unit of work in a
VB.NET program.
A statement is always
terminated with a line break.
Except when the _ character is
used.
Console.WriteLine();
This line outputs the string
Welcome to VB.NET! followed
by a new line on the screen.
'
'
'
'
Imports System
Imports A = System.Console
Namespace VBNETSchool
Public Class VBNetOne
''' <summary>
''' </summary>
''' <param name="args"></param>
Public Shared Sub Main(ByVal args As String())
' print a message
A.WriteLine _
("Welcome to VB.NET!")
End Sub
End Class
End Namespace
VB.NET Keywords
Keywords are an essential part of language definition as they implement
specific features of the language.
AddHandler
AddressOf
Alias
And
As
Assembly
Auto
Boolean
ByRef
Byte
AndAlso
Ansi
ByVal
Call
Case
Catch
While
Unicode
CBool
CByte
CChar
CDate
With
Until
CDec
CDbl
Char
CInt
WithEvents
Variant
Class
CLng
CObj
Const
WriteOnly
When
CShort
CSng
CStr
CType
To
Sub
Date
Decimal
Declare
Default
True
SyncLock
Delegate
Dim
DirectCast
Do
Try
Then
Double
Each
Else
ElseIf
TypeOf
Throw
10
VB.NET Keywords
Event
Exit
False
Finally
Stop
Short
End
Enum
Erase
Error
Step
Shared
For
Friend
Function
Get
String
Single
GetType
GoSub
GoTo
Handles
Structure
Static
If
Implements
Imports
In
Return
ReDim
Inherits
Integer
Interface
Is
Select
REM
Let
Lib
Like
Long
Set
RemoveHandler
Loop
Me
Mod
Module
Shadows
Resume
MustInherit
MustOverride
MyBase
MyClass
Protected
ParamArray
Namespace
New
Next
Not
Public
Preserve
Nothing
NotInheritable
NotOverridable
Object
RaiseEvent
Private
On
Option
Optional
Or
ReadOnly
Property
OrElse
Overloads
Overridable
Overrides
11
Identifiers
An identifier is the name given by a programmer to a variable, statement
label, method, class, and interface.
An identifier must begin with a letter
Write_Print
Literals
Literals are the way in which the values that are stored in variables are
represented.
VB.NET Literals
Numeric Literals
Integer Literals
Boolean Literals
Real Literals
Character Literals
String Literals
13
14
Data Types
VB.NET Data Types
Value Types
Reference Types
Pointers
Predefined
User-defined
Predefined
User-defined
Types
Types
Types
Types
Integers
Enumerations
Objects
Classes
Real Integers
Structures
Strings
Arrays
Booleans
Delegates
Characters
Interfaces
15
Boolean Types
Numeric Types
Character Types
Floating Point
Integral
Decimal
Types
Types
Types
Signed
Unsigned
Types
Types
16
Data Types
Signed Integers
UnSigned Integers
sbyte
byte
short
int
long
ushort
uint
ulong
17
Floatingpoint types
Types
float
double
Floatingpoint types support a special value known as Not-aNumber(NaN). NaN is used to represent results of operations such as
dividing zero by zero, where an actual number is not produced.
18
(Continued)
Decimal Type
Is a high precision 128-byte data type that is designed for use in financial and
monetary calculations.
It can store values in the range 1.0 10e28 to 7.9 10e28.
To specify a number to be decimal type, append the character M (or m) to the
value e.g 123.45M.
Boolean Types
Character Types
(Continued)
Structures
Syntax:
member1 as data
member2 as data
End Structure
Structure struct-name
E.g..
Structure Student
Public Name As String
Public ID As Integer
Public TotalMark As Double
End Structure
s1.Name = John
S1.RollNumber = 0200789
20
(continued)
Enumerations
Syntax:
Enum enum-name
word1
word2
word3
End Enum
E.g.
Enum Color
Red = 10
Blue = 20
Green = 100
End Enum
21
Class
Interfaces
Delegates
Arrays
22
Variables
Default Values
23
Constant Variables
Variables whose values do not change during execution of a program.
Is illegal
Const m As Integer = 10
Const age As Integer = m * 5
Is illegal
Dim m As Itneger= 10
Const age as Integer = m * 5
24
21
initial
value
reference
type
initial
value
25
MEMORY
type
age
1
07
initialization/assignment
age =
17
Identifier value
name
stack
26
myCar
memory address
location
type
Identifier
name
reference
The heap
Identifier
name
Bumble Bee
Car object
27
Scope of Variable
..
Member Variables
Block2
Local Variables
Block1
Dim x as Integer =0
Public Sub TestSub
Dim n As Integer =5 Ok
Dim x As Integer =0 Ok
.
End Sub
Block3
Dim m As Integer = 20 ok
x = m ok
End Sub
End Class
28
UnBoxing
Console.WriteLine(obj1)
End Sub
End Sub
29
Agenda
Fundamentals
Operators
Flow Controls
30
Unary operators
Arithmetic operators
String operators
Relational operators
Conditional operators
Logical operators
Assignment operators
Shift operators
Bitwise operators
Primitive Casting
31
Unary Operators
Unary operators use only one operand.
+
Positive sign
Negative sign
Sample code:
Dim num as Integer =10
System.Console.WriteLine("setting signs)
System.Console.WriteLine(+num)
System.Console.WriteLine(-num)
Sample output:
setting signs...
10
-10
32
Arithmetic Operators
Arithmetic operators are used for basic mathematical operations.
+
Add
Subtract
Multiply
Divide
Mod
Modulo, remainder
Sample code:
Dim num1 as Integer =15
Dim num2 as Integer =10
System.Console.WriteLine("calculating...")
System.Console.WriteLine(num1 + num2)
System.Console.WriteLine(num1 - num2)
System.Console.WriteLine(num1 * num2)
System.Console.WriteLine(num1 / num2)
System.Console.WriteLine(num1 Mod num2)
Sample output:
calculating...
25
5
150
1
5
33
String Operators
The string operator (+, &) is used to concatenate operands.
If one operand is string, the other operands are converted to string.
Sample output:
My full name is: Ford, Henry D.
You can call me Henry!
I'm 21 years old.
34
Relational Operators
Relational operators are used to compare values.
boolean values cannot be compared with non-boolean values.
Only object references are checked for equality, and not their states.
Objects cannot be compared with null.
null is not the same as .
<
<=
>
>=
=
<>
Less than
Less than or equal to
Greater than
Greater than or equal to
Equals
Not equals
35
Relational Operators
Sample code:
Dim name1 As String = "Marlon"
Dim weight1 As Integer = 140, height1 As Integer = 74
Dim name2 As String = "Katie"
Dim weight2 As Integer = 124, height2 As Integer = 78
Dim isLight As Boolean = weight1 < weight2, isLightEq As Boolean = weight1 <= weight2
System.Console.WriteLine("Is " + name1 + " lighter than " + name2 + "? " + isLight)
System.Console.WriteLine("Is " + name1 + " lighter or same weight as " + name2 + "? " +
isLightEq)
Dim isTall As Boolean = height1 > height2, isTallEq As Boolean = height1 >= height2
System.Console.WriteLine("Is " + name1 + " taller than " + name2 + "? " + isTall)
System.Console.WriteLine("Is " + name1 + " taller or same height as " + name2 + "? " +
isTallEq)
Dim isWeighEq As Boolean = weight1 = weight2, isTallNotEq As Boolean = height1 <> height2
System.Console.WriteLine("Is " + name1 + " same weight as " + name2 + "? " + isWeighEq)
System.Console.WriteLine("Is " + name1 + " not as tall as " + name2 + "? " + isTallNotEq)
System.Console.WriteLine("So who is heavier?")
System.Console.WriteLine("And who is taller?") Is Marlon lighter than Katie? false
Sample output:
Logical Operators
Logical operators are used to compare
boolean expressions.
Not inverts a boolean value.
AND and OR evaluate both operands.
Truth Table
Op1
Op2
Not Op1
Op1 And
Op2
Op1 Or
Op2
false
false
true
false
false
false
true
true
false
true
true
false
false
false
true
true
true
false
true
true
Not
logical NOT
AND
logical AND
OR
logical OR
AndAlso
OrElse
Same as Or except
that once a false is
found it will not
continue checking the
succeeding
conditions.
37
Logical Operators
Sample output:
Sample code:
Assignment Operators
Assignment operators
are used to set the
value of a variable.
=
Assign
+=
-=
*=
/=
&=
|=
OR and assign
^=
Sample code:
Sample output:
Gross Sales: 240.0
Tax: 24.0
Discount: 39.6
Please pay: 224.4
39
40
Summary of Operators
Evaluation order of operators in VB.NET is as follows:
Unary
Arithmetic
Comparison
Bitwise
Logical Operators
Assignment
42
Agenda
Fundamentals
Operators
Flow Controls
43
Flow Controls
If-Else statement
Select statement
While statement
Do-While statement
For statement
For Each statement
Exit statement
Continue statement
44
If-Else
If-Else performs
statements based on two
conditions.
Condition should result
to a boolean expression.
Syntax:
End If
If condition1 Then
ElseIf condition2 Then
Else
Output:
You're a teenager.
45
Select
Select performs
statements based on
multiple conditions.
exp can be char byte
short int, val should be
a unique constant of exp.
Case statements falls
through the next case
unless a exit is
encountered.
Case Else is executed if
none of the other cases
match the exp.
Syntax:
Select Case exp
Case val
'Do something
Case val
'do something
Case Else
'If no match it will pass here
End Select
Example:
Dim sex As Char = "M"
Select Case sex
Case "M"
System.Console.WriteLine("I'm a male.")
Case "F"
System.Console.WriteLine("I'm a
female.")
Case Else
System.Console.WriteLine("I am what I
am!")
End Select
Output:
I'm a male.
46
While
While performs statements repeatedly while condition remains true.
Syntax:
Example:
Output:
While condition
'Do something
End While
Dim ctr As Integer = 10
While ctr > 0
System.Console.WriteLine("Timer: " + ctr.ToString)
ctr -= 1
End While
Timer:
Timer:
Timer:
Timer:
Timer:
Timer:
Timer:
Timer:
Timer:
Timer:
10
9
8
7
6
5
4
3
2
1
47
Example:
Output:
Do
'this will run at least once
Loop While condition
Dim ctr As Integer = 0
Do
System.Console.WriteLine("Timer: " + ctr)
ctr += 1
Loop While ctr < 10
Timer:
Timer:
Timer:
Timer:
Timer:
Timer:
Timer:
Timer:
Timer:
Timer:
0
1
2
3
4
5
6
7
8
9
48
For Next
For enable us to execute
a series of expressions
multiple numbers of times
Syntax:
For init To final Step inc
'do something
Next
Example:
For age As Integer = 18 To 29
System.Console.WriteLine("Enjoy life while you're
" + age)
Next
Output:
Enjoy
Enjoy
Enjoy
Enjoy
Enjoy
Enjoy
Enjoy
Enjoy
Enjoy
Enjoy
Enjoy
Enjoy
life
life
life
life
life
life
life
life
life
life
life
life
while
while
while
while
while
while
while
while
while
while
while
while
you're
you're
you're
you're
you're
you're
you're
you're
you're
you're
you're
you're
18
19
20
21
22
23
24
25
26
27
28
29
49
For Each
For Each() is similar to for statement
but implemented differently.
type and variable declare the
iteration variable. During execution,
the iteration variable represents the
array element (or collection element
in case of collections) for which an
iteration is currently being performed.
In is a keyword.
collection must be an array or
collection type and an explicit
conversion must exist from the
element type of the collection to the
type of the iteration variable.
Syntax:
For Each variable In collection
'Statement here
Next
Example:
Dim arrayint As Integer() = {11, 22,
33, 44}
For Each m As Integer In arrayint
System.Console.WriteLine(" " + m)
Next
System.Console.WriteLine()
Output:
11
22
33
44
50
Exit
Exit exits loops and other conditional statements.
Syntax:
Example:
For
Sub
Function
While
Do
Select
51
Continue
Output:
52
return
The return branching statement is used to exit from the current method.
There are two forms:
Example 1:
return <value>;
return;
53
Key Points
A VB.NET source file can include using, Namespace and class
declarations in that order
Identifiers are case-sensitive
VB.NET keywords cannot be used as identifiers
Each variable must be declared with a data type
There are 13 primitive data types: sbyte, short, int, long,
byte, ushort, uint, ulong, float, double, decimal,
char, boolean
There are 4 reference data types: classes, interfaces, delegates, and
arrays
Use unary, arithmetic operators for basic mathematical operations
Use string operator to concatenate strings
54
55
56