mcav_vbdotnet_session3
TRANSCRIPT
-
8/3/2019 MCAV_VBDOTNET_Session3
1/28
VISUAL BASIC.NET
-
8/3/2019 MCAV_VBDOTNET_Session3
2/28
Methods - Sub Procedures and Functions
A sub procedure is a method that does notreturn any values. In VB.Net, it is defined usingthe Sub keyword. A sub procedure may or maynot accept parameters. For example,
Sub ShowCurrentTime()Console.WriteLine("The current time is: " &
DateTime.Now)
End Sub
-
8/3/2019 MCAV_VBDOTNET_Session3
3/28
Methods - Sub Procedures and Functions
A function is a type of method that returnsvalues. A function, like a sub-procedure may ormay not accept parameters. For example,
Function FindSum(ByVal num1 As Integer,
ByVal num2 As Integer) As IntegerDim sum As Integer = num1 + num2
Return sum
End Function
-
8/3/2019 MCAV_VBDOTNET_Session3
4/28
Passing Arguments (ByVal , ByRefkeyword)
Call by Reference makes the changes reflectback in the original variables .
Call By Value works with copies of originalvalues and changes are not reflected back inoriginal values.
-
8/3/2019 MCAV_VBDOTNET_Session3
5/28
The OptionalKeyword
The parameters of VB.Net methods (subprocedures and functions) can be marked as'Optional'.
The optional parameters must be the final
parameter of a method. A method with an Optional parameter may be
called either with the optional parameter or withoutthe optional parameter.
An Optional parameter is that they must bespecified with the default value.
-
8/3/2019 MCAV_VBDOTNET_Session3
6/28
Loops and control structures
The If Statement
The Select Case Statement
The Do While and Loop While loops
The For Loop
-
8/3/2019 MCAV_VBDOTNET_Session3
7/28
If Else
If (Condition)Then
Statements executed if condition is true
Else
Statements executed if condition is false
EndIf
We can also have Else If block in If Elsestatements
Every If statement must contain a then and anend if
-
8/3/2019 MCAV_VBDOTNET_Session3
8/28
The Select Case statement
Select Case varCase 1
stmt1 // executed if var = 1Case 2
stmt2 // executed if var = 2Case Else
stmt3 // executed if var is other than 1 and 2End Select
The select case statement is a lot like the switchstatement in Java
It can use ANY data type in VB
-
8/3/2019 MCAV_VBDOTNET_Session3
9/28
Using To and Is in an
ExpressionList To keyword: specifies a range of minimum and
maximum values
Case 1 To 5
Is keyword: specifies a range of values whenyou know only one value, either the minimum orthe maximum
Case Is > 10
-
8/3/2019 MCAV_VBDOTNET_Session3
10/28
The ForNext Loop
We use the ForNext statement to code a loop
that repeats for a specific number of times
-
8/3/2019 MCAV_VBDOTNET_Session3
11/28
The ForNext Loop
counteris a numeric variable that keepstrack of how many times the loopinstructions are repeated
startvalue, endvalue, and stepvalue Must be numeric Can be positive or negative, integer or non-
integer
Default stepvalueis 1
-
8/3/2019 MCAV_VBDOTNET_Session3
12/28
The DoLoop Statement
Unlike the ForNext statement, theDoLoop statement can be used to
code both a pretest loop and aposttest loop
-
8/3/2019 MCAV_VBDOTNET_Session3
13/28
The DoLoop Statement
-
8/3/2019 MCAV_VBDOTNET_Session3
14/28
The DoLoop Statement
-
8/3/2019 MCAV_VBDOTNET_Session3
15/28
Do While Loop
Do While(a0)Console.Writeline(a)
a = a 1
Loop
Do
Console.Writeline(a)
a = a 1Loop While(a0)
-
8/3/2019 MCAV_VBDOTNET_Session3
16/28
Do Until Loop
Do Until(a=0)Console.Writeline(a)
a = a 1
Loop
Do
Console.Writeline(a)
a = a 1Loop Until(a=0)
-
8/3/2019 MCAV_VBDOTNET_Session3
17/28
Comparison Operators
= Is equal to
> Is Greater Than
>= Is Greater Than or Equal to
< Is Less Than
-
8/3/2019 MCAV_VBDOTNET_Session3
18/28
Comparison Operators
Comparison operators are also referred to asrelational operators
All expressions containing a relational operatorwill result in either a true or false answer only
Comparison operators are evaluated from left toright, and are evaluated after any mathematicaloperators
-
8/3/2019 MCAV_VBDOTNET_Session3
19/28
Comparison Operators(continued)
10 + 3 < 5 * 2
5 * 2 is evaluated first,giving 10
10 + 3 is evaluatedsecond, giving 13
13 < 10 is evaluatedlast, giving false
7 > 3 * 4 / 2
3 * 4 is evaluatedfirst, giving 12
12 / 2 is evaluatedsecond, giving 6
7 > 6 is evaluatedlast, giving true
-
8/3/2019 MCAV_VBDOTNET_Session3
20/28
Logical Operators
Not Reverses the truth value of condition; false becomestrue and true becomes false.
1
And All conditionsconnected by the And operator must betrue for the compound conditionto be true.
2
AndAlso All conditionsconnected by the AndAlso operatormust be true for the compound conditionto be true.
2
Or Only one of the conditionsconnected by the Oroperator needs to be true for the compound conditionto be true.
3
OrElse Only one of the conditionsconnected by the OrElseoperator needs to be true for the compound conditionto be true.
3
Xor One of the conditionsconnected by Xor must be truefor the compound condition to be true.
4
-
8/3/2019 MCAV_VBDOTNET_Session3
21/28
Short-circuiting evaluations
VB.NET features two logical operators that helpmake programming more logical. These newoperators [AndAlso and OrElse] add a lot to theold And and Or operators.
They offer advantages in two general categories: We can avoid executing part of a logical
expression to avoid problems.
We can optimize code by not executing any
more of a compound expression than required. AndAlso and OrElse are pretty much like And and
Or except that they will "short circuit" anexpression once the outcome is guaranteed.
-
8/3/2019 MCAV_VBDOTNET_Session3
22/28
-
8/3/2019 MCAV_VBDOTNET_Session3
23/28
Logical Operators
Truth table for And operator
If condition1 is And condition2is
Value of Result is
True True True
True False False
False True False
False False False
Result = condition1 And Condition2
-
8/3/2019 MCAV_VBDOTNET_Session3
24/28
Logical Operators
Truth table for AndAlso operator
If condition1 is And condition2is
Value of Result is
True True True
True False False
False(not
evaluated)False
Result = condition1 AndAlso Condition2
-
8/3/2019 MCAV_VBDOTNET_Session3
25/28
Logical Operators
Truth table for Or operator
If condition1 is And condition2is
Value of Result is
True True True
True False True
False True True
False False False
Result = condition1 Or Condition2
-
8/3/2019 MCAV_VBDOTNET_Session3
26/28
Logical Operators
Truth table for OrElse operator
If condition1 is And condition2is
Value of Result is
True (not evaluated) True
False True True
False False False
Result = condition1 OrElse Condition2
-
8/3/2019 MCAV_VBDOTNET_Session3
27/28
Logical Operators
Truth table for Xor operator
If condition1 is And condition2is
Value of Result is
True True True
True False False
False True True
False False False
Result = condition1 Xor Condition2
-
8/3/2019 MCAV_VBDOTNET_Session3
28/28
How to add command line
arguments in Visual Studio IDE ??