Instrukcja if
If condition Then
[statements]
[ElseIf condition-n Then
[elseifstatements] …]
[Else [elsestatements]]
End If
Przykład instrukcji if
Module Module1
Sub Main()
Dim intInput As Integer
System.Console.WriteLine("Enter an integer…")
intInput =
Val(System.Console.ReadLine())
If intInput = 1 Then
System.Console.WriteLine("Thank you.")
ElseIf intInput = 2 Then
System.Console.WriteLine("That's fine.")
ElseIf intInput = 3 Then
System.Console.WriteLine("Too big.")
Else System.Console.WriteLine("Not a number I know.")
End If
End Sub
End Module
Instrukcja Select Case
Select Case testexpression
[Case expressionlist-n
[statements-n]]
…
[Case Else [elsestatements]]
End Select
Przykład Select Case
Module Module1
Sub Main()
Dim intInput As Integer System.Console.WriteLine("Enter an integer…")
intInput = Val(System.Console.ReadLine())
Select Case intInput
Case 1
System.Console.WriteLine("Thank you.")
Case 2
System.Console.WriteLine("That's fine.")
Case 3
System.Console.WriteLine("OK.")
Case 4 To 7
System.Console.WriteLine("In the range 4 to 7.")
Case Is > 7
System.Console.WriteLine("Definitely too big.")
Case Else
System.Console.WriteLine("Not a number I know.")
End Select
End Sub
End Module
Instrukcja For
For index = start To end [Step
step] [statements]
[Exit For]
[statements]
Next [index]
Przykład For
Module Module1
Sub Main()
Dim intLoopIndex As Integer
For intLoopIndex = 0 To 3
System.Console.WriteLine _
("Hello from Visual Basic")
Next intLoopIndex
End Sub
End Module
Instrukcja Do…Loop
Z warunkiem na początku:
Do [{While | Until} condition ]
[statements]
[Exit Do]
[statements]
Loop
Instrukcja Do…Loop
Z warunkiem na końcu:
Do
[statements]
[Exit Do]
[statements]
Loop
[{While | Until} condition ]
Przykład Do…Loop
Module Module1
Sub Main()
Dim strInput As String
Do Until
UCase(strInput)
= "STOP"
System.Console.WriteLine _
("What should I do?")
strInput = System.Console.ReadLine()
Loop
End Sub
End Module
Instrukcja While
While condition
[statements]
End While
Przykład:
Dim i As Integer = 0
While i<10
System.Console.WriteLine (i)
i +=1
End While
Imports
Imports
System.Console
Module Module1
Sub Main()
WriteLine
("Hello from Visual
Basic")
End Sub
End Module
Operacje na łańcuchach
Module Module1
Sub Main()
Dim strText1 As String = "welcome to visual
basic"
Dim strText2 As String
Dim strText3 As String
strText2 =
UCase
(strText1)
strText3 = strText1.
ToUpper
System.Console.WriteLine(strText2)
System.Console.WriteLine(strText3)
End Sub
End Module
Operacje na łańcuchach cd.
Module Module1
Sub Main()
Dim strText1 As String = "Hey, look here!„
Dim strText2 As String
Dim strText3 As String
strText2 =
Mid(strText1, 6, 4)
strText3 =
strText1.Substring(5, 4)
System.Console.WriteLine(strText2)
System.Console.WriteLine(strText3)
End Sub
End Module
Łańcuchy ↔ liczby
Module Module1
Sub Main()
Dim strText1, strText2 As String =
"1234„
Dim intValue1 As Integer
intValue1 =
Val(strText1)
strText2 =
Str(intValue1)
System.Console.WriteLine(strText2)
End Sub
End Module
Znaki ↔ liczby
• Asc(
"A"
) returns 65
• Chr(65) returns
"A"
Dim OneCharacter As Char =
"
a
pple"
Funkcje matematyczne
Imports System.Math
Module Module1
Sub Main()
System.Console.WriteLine(
Sin
(1.046)
)
End Sub
End Module
Abs, Cos, Exp, Log, Sqr, Round, …
Daty i czas
Imports System.Math
Module Module1
Sub Main()
Dim FirstDate As Date FirstDate =
#12/31/2007#
System.Console.WriteLine("New date: " &
DateAdd(DateInterval.Month, 22, FirstDate)
)
While
TimeString
<
"15:45:00"
End While
Beep()
End Sub
End Module
Wyliczenia
Imports System.Console
Module Module1
Enum Days
Sunday = 1
Monday = 2
…
Friday = 6
Saturday = 7
End Enum
Sub Main()
WriteLine("Friday is day " &
Days.Friday
)
End Sub
End Module
Friday is day 6
Tablice jednowymiarowe
Dim name (max_ind) As type
Przykład:
Dim persons(
4
) As
String
persons(1) = "Bobby"
' persons(
5
) = "David" Throws
' System.IndexOutOfRange
Exception
Dim Data() = {10, 3, 2}
ReDim
Preserve
persons(10)
Tablice dwuwymiarowe
Dim name (max_ind1, max_ind2) As type
Przykład:
Dim tab(
2, 3
) As
Integer
tab(1, 1) = 15
Dim tabx()() As Integer = { _
New
Integer(4) {},_
New
Integer(1) {},_
New
Integer(2) {} }
tabx(0)(3) = 5