Vous êtes sur la page 1sur 3

Visual Basic .

NET Programming for Beginners

MESSAGE BOX

MessageBox.Show("Your Message Here",”Caption”, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

CONDITIONAL LOGIC – IF STATEMENTS

If x=1 then
[command]
Else
[command]
End If

SELECT CASE STATEMENTS

Select Case agerange


Case 1
Messagebox.show(“Still a Baby”)

Case 16 To 21
Messagebox.show(“Still Young”)
Case 50 To 64
Messagebox.show(“Start Lying”)
Case Else
Messagebox.show(“Nothing”)

End Select

CONDITIONAL OPERATORS
Operator Meaning
> This symbol means Is Greater Than and is used like this:
If number > 10 Then
MsgBox "The Number was Greater Than 10"
End If

< This symbol means Is Less Than and is used like this:
If number <10 Then
MsgBox "The Number was Less Than 10"
End If

>= These symbols mean Is Greater Than or Equal to, and are used like this:
If number >= 10 Then
MsgBox "The Number was 10 or Greater"
End If

<= These symbols mean Is Less Than or Equal to, and are used like this:
If number <= 10 Then
MsgBox "The Number was 10 or Less"
End If

And You can combine the logical operators with the word And. Like this:
If number > 5 And number < 15 Then
MsgBox "Greater than 5 And Less than 15"
End If

Or You can combine the logical operators with the word Or. Like this:
If number > 5 Or number < 15 Then
MsgBox "Greater than 5 Or Less than 15"
End If

<> These symbols mean Is Not Equal to, and are used like this:
If number1 <> number2 Then
MsgBox "number1 is not equal to number2"
End If

1
LOOPS IN VB.NET

A loop is something that goes round and round and round. If someone told you to move your finger around in a loop, you'd know what to do
immediately. In programming, loops go round and round and round, too. In fact, they go round and round until you tell them to stop. You can
programme without using loops. But it's an awful lot easier with them. Consider this.

You want to add up the numbers 1 to 4: 1 + 2 + 3 + 4. You could do it like this

Dim answer As Integer

answer = 1 + 2 + 3 + 4

MsgBox answer

Fairly simple, you think. And not much code, either. But what if you wanted to add up a thousand numbers? Are you really going to type them all
out like that? It's an awful lot of typing. A loop would make life a lot simpler.

But don't get hung up too much on the name of the Loop. Just remember what they do: go round and round until you tell them to stop.

1) FOR LOOP

Dim I as integer
For i = startNumber To endNumber
Msgbox i
Next i

***You can exit loop by EXIT FOR command

2) DO LOOP

Dim number as integer


number=1
Do While number < 5
MsgBox number
number = number + 1
Loop

--------------

number=1
Do Until number < 5
MsgBox number
number = number + 1
Loop

2
BASIC MATH SYMBOLS

+ The Plus sign adds numbers together

- The minus sign takes one number away from another

* The symbol above the number 8 on your keyboard tells Visual Basic to multiply two numbers

/ The forward slash on your keyboard is the divide by symbol

= The equals sign

STRING MANIPULATION

Dim strFirstName as string


Dim strLastName as string
Dim strFullName as string

strFirstName=”Jonathan Bryan”
strLastName=”Gonzales”
strFullName=strFirstName & “ “ & strLastName

strFullName=ucase(strFullName) Result : JONATHAN BRYAN GONZALES

strLastName=”Gonzales ”  with spaces at the end


strLastName=Trim(strLastName)  results in “Gonzales”  no spaces

INSTR METHOD

The InStr( ) method of string variables tells you what the position of one string is inside another. For example, if your string was "me@me.com"
and you wanted to know if the string contained the @ symbol, you could use InStr( ) Method. You would use it like this

FirstString = "me@me.com"
SecondString = "@"
Dim position as integer
position = InStr(FirstString, SecondString)

The variable FirstString is the string we want to search; SecondString is what we want to search for. You can specify a starting position for the
search to begin. If you do, this number goes at the start (the default is zero):
position = InStr(1, FirstString, SecondString)

SUBSTRING METHOD

Another useful string method is Substring. This allows you to grab one string within another. (For example, if you wanted to grab the ".com" from
the email address "me@me.com")
In between the round brackets of Substring( ), you specify a starting position, and then how many characters you want to grab (the count starts
at zero again). Like this:
Syntax : Substring(string, start position, length)

Dim Email as String


Dim DotCom as String

Email = "me@me.com"
DotCom = Substring(Email, 5, 4)

MsgBox(DotCom)
The message box would then display the characters grabbed from the string, in this case the ".com" at the end (start at position 5 in the string
and grab 4 characters).

Vous aimerez peut-être aussi