Vous êtes sur la page 1sur 28

VISUAL BASIC.

NET

Methods - Sub Procedures and Functions


A sub procedure is a method that does not return any values. In VB.Net, it is defined using the Sub keyword. A sub procedure may or may not accept parameters. For example, Sub ShowCurrentTime() Console.WriteLine("The current time is: " & DateTime.Now) End Sub

Methods - Sub Procedures and Functions


A function is a type of method that returns values. A function, like a sub-procedure may or may not accept parameters. For example, Function FindSum(ByVal num1 As Integer, ByVal num2 As Integer) As Integer Dim sum As Integer = num1 + num2 Return sum End Function

Passing Arguments (ByVal , ByRef keyword)


Call by Reference makes the changes reflect back in the original variables . Call By Value works with copies of original values and changes are not reflected back in original values.

The Optional Keyword

The parameters of VB.Net methods (sub procedures 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 without the optional parameter. An Optional parameter is that they must be specified with the default value.

Loops and control structures


The If Statement The Select Case Statement The Do While and Loop While loops The For Loop

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 Else statements Every If statement must contain a then and an end if

The Select Case statement


Select Case var Case 1 stmt1 // executed if var = 1 Case 2 stmt2 // executed if var = 2 Case Else stmt3 // executed if var is other than 1 and 2 End Select The select case statement is a lot like the switch statement in Java It can use ANY data type in VB

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 when you know only one value, either the minimum or the maximum Case Is > 10

The ForNext Loop


We use the ForNext statement to code a loop that repeats for a specific number of times

The ForNext Loop

counter is a numeric variable that keeps track of how many times the loop instructions are repeated startvalue, endvalue, and stepvalue
Must be numeric Can be positive or negative, integer or noninteger Default stepvalue is 1

The DoLoop Statement


Unlike the ForNext statement, the DoLoop statement can be used to code both a pretest loop and a posttest loop

The DoLoop Statement

The DoLoop Statement

Do While Loop
Do While(a<>0) Console.Writeline(a) a=a1 Loop Do Console.Writeline(a) a=a1 Loop While(a<>0)

Do Until Loop
Do Until(a=0) Console.Writeline(a) a=a1 Loop Do Console.Writeline(a) a=a1 Loop Until(a=0)

Comparison Operators
= > >= < <= Is equal to Is Greater Than Is Greater Than or Equal to Is Less Than Is Less Than or Equal to

<>

Is Not Equal to

Comparison Operators

Comparison operators are also referred to as relational operators All expressions containing a relational operator will result in either a true or false answer only Comparison operators are evaluated from left to right, and are evaluated after any mathematical operators

Comparison Operators (continued)


10 + 3 < 5 * 2

7>3*4/2

5 * 2 is evaluated first, giving 10


10 + 3 is evaluated second, giving 13 13 < 10 is evaluated last, giving false

3 * 4 is evaluated first, giving 12


12 / 2 is evaluated second, giving 6 7 > 6 is evaluated last, giving true

Logical Operators
Not Reverses the truth value of condition; false becomes true and true becomes false. 1

And
AndAlso Or

All conditions connected by the And operator must be true for the compound condition to be true.
All conditions connected by the AndAlso operator must be true for the compound condition to be true. Only one of the conditions connected by the Or operator needs to be true for the compound condition to be true. Only one of the conditions connected by the OrElse operator needs to be true for the compound condition to be true. One of the conditions connected by Xor must be true for the compound condition to be true.

2
2 3

OrElse

Xor

Short-circuiting evaluations
VB.NET features two logical operators that help make programming more logical. These new operators [AndAlso and OrElse] add a lot to the old 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" an expression once the outcome is guaranteed.

Logical Operators

Truth table for Not operator


Result = Not Condition If condition is True Value of Result is False

False

True

Logical Operators

Truth table for And operator


Result = condition1 And Condition2 If condition1 is And condition2 is True True Value of Result is True

True
False

False
True

False
False

False

False

False

Logical Operators

Truth table for AndAlso operator


Result = condition1 AndAlso Condition2 If condition1 is And condition2 is True True Value of Result is True

True
False

False
(not evaluated)

False
False

Logical Operators

Truth table for Or operator


Result = condition1 Or Condition2 If condition1 is And condition2 is True True False True False True Value of Result is True True True

False

False

False

Logical Operators

Truth table for OrElse operator


Result = condition1 OrElse Condition2

If condition1 is
True False

And condition2 is
(not evaluated) True

Value of Result is
True True

False

False

False

Logical Operators

Truth table for Xor operator


Result = condition1 Xor Condition2 If condition1 is And condition2 is True True False False True False True False Value of Result is True False True False

How to add command line arguments in Visual Studio IDE ??

Vous aimerez peut-être aussi