Przykład procedury
Module Module1
Sub Main()
DisplayMessage("Hello from Visual Basic")
End Sub
Sub DisplayMessage(ByVal strText As
String)
System.Console.WriteLine(strText)
End Sub
End Module
Schemat definiowania
procedury
Sub name [(arglist)]
[ statements ]
[ Exit Sub ]
[ statements ]
End Sub
Przykład funkcji
Module Module1
Sub Main()
Dim intValue As Integer = 2
System.Console.WriteLine("
{0}
+
{1}
=
{2}
",
intValue, _
intValue, Add(intValue, intValue))
End Sub
Function Add(ByVal i1 As Integer, ByVal i2 As
Integer)_
As Long
Return
i1 + i2
End Function
End Module
Schemat definiowania
funkcji
Function name [(arglist)] As type
[ statements ]
[ Exit Function ]
[ statements ]
End Sub
Musi zawierać
return expr
.
Przekazywanie parametrów
• ByVal
- przez wartość. Stosowany
dla parametrów „wejściowych”. Do
procedury/funkcji pzrekazywana
jest
wartość
parametru aktualnego.
• ByRef
– przez referencję.
Stosowany dla parametrów
wyjściowych. Do procedury/funkcji
przekazywana jest
referencja
do
parametru aktualnego (który
musi
być zmienną
).
Przykład przekazywania
parametrów
Sub TestFunc(
ByVal x
As Integer,
ByRef y
As _
Integer,
ByRef z
As Integer)
x += 1
y += 1
z = 5
End Sub
Dim a = 1, b = 1, c As Integer
' c set to zero by default
TestFunc(
a, b, c
)
Console.WriteLine("{0} {1} {2}", a, b, c)
'
1
2 5
Wartości opcjonalne
parametrów
Module Module1
Sub Main()
DisplayMessage()
End Sub
Sub DisplayMessage(
Optional ByVal
strText _ As String
= "Hello from Visual
Basic"
)
System.Console.WriteLine(strText)
End Sub
End Module
Przykład parametru
opcjonalnego
' Optional parameters must be listed
last and must have a default value
Sub SayHello(ByVal name As String, _
Optional ByVal
prefix
As String =
"")
Console.WriteLine("Greetings, " &
prefix & " " & name)
End Sub
SayHello("Nowak", "Dr.")
SayHello("Kowalski")
Zmienna liczba parametrów
Module Module1
Sub Main()
DisplayMessage("First message:",
"Hi"
)
DisplayMessage("Second message:",
"Hello", "there"
)
Dim TextArray() As String = {"Hello", "from", "Visual", _
"Basic"}
DisplayMessage("Third message:",
TextArray
)
End Sub
Sub DisplayMessage(ByVal Title As String,
ByVal ParamArray _
MessageText() As String
)
Dim intLoopIndex As Integer
System.Console.WriteLine(Title)
For intLoopIndex = 0 To
UBound(MessageText)
System.Console.WriteLine(MessageText(intLoopIndex))
Next intLoopIndex
End Sub
End Module
Zmienne statyczne
Module Module1
Sub Main()
Dim intLoopIndex As Integer, intValue = 0
For intLoopIndex = 0 To 4
intValue =
Counter()
Next intLoopIndex
System.Console.WriteLine(
intValue
)
End Sub
Function Counter() As Integer
Dim intCountValue As Integer
intCountValue += 1
Return
intCountValue
End Function
End Module
Przykład zmiennej liczby
parametrów
Function
Sum
(ByVal
ParamArray nums
_
As Integer()) As Integer
Sum
= 0
For Each i As Integer In nums
Sum
+= i
Next
End Function
Dim total As Integer = Sum(
4, 3, 2, 1
)
Zmienne statyczne
Module Module1
Sub Main()
Dim intLoopIndex As Integer, intValue = 0
For intLoopIndex = 0 To 4
intValue =
Counter()
Next intLoopIndex
System.Console.WriteLine(
intValue
)
End Sub
Function Counter() As Integer
Static
intCountValue As Integer
intCountValue += 1
Return
intCountValue
End Function
End Module
Zakres (ang. scope)
widoczności
• Block
scope
- available only within
the code block in which it is declared
• Procedure
scope
- available only
within the procedure in which it is
declared
• Module
scope
- available to all code
within the module, class, or
structure in which it is declared
• Namespace
scope
- available to all
code in the namespace
Obsługa wyjątków
Try
[ tryStatements ]
[Catch [ exception1 [ As type1 ] ] [ When
expression1 ]
catchStatements1 [Exit Try] ]
[Catch [ exception2 [ As type2 ] ] [When expression2 ]
catchStatements2 [ Exit Try ] ]
…
[Catch [ exceptionn [ As typen ] ] [ When
expressionn ]
catchStatementsn ] [ Exit Try ] ]
[ Finally
[ finallyStatements ]
End Try
Przykład obsługi wyjątków
Sub Main()
Dim int3 As Integer
Try
int3 = …
System.Console.WriteLine("The answer is {0}", int3)
Catch
e As System.ArgumentOutOfRangeException
System.Console.WriteLine("Exception: Argument out of" _
& " range!")
Catch
e As System.ArgumentException
System.Console.WriteLine("Exception: Invalid argument„ _
& " value!")
Catch
e As Exception
System.Console.WriteLine("Exception occurred!")
End Try
End Sub
Przykład z when
Sub Main()
Dim int1 = 0, int2 = 1, int3 As Integer
Try int3 = int2 / int1
System.Console.WriteLine( _
"The answer is {0}", int3)
Catch
When Err.Number = 6
System.Console.WriteLine( _
"Exception: Arithmetic overflow!")
End Try
End Sub
Przykład z finally
Sub Main()
…
Try
…
Catch e As System.ArgumentOutOfRangeException
System.Console.WriteLine("Exception: Argument out of" _
& " range!")
…
Catch e As Exception
System.Console.WriteLine("Exception occurred!")
Finally
System.Console.WriteLine("Execution of sensitive code " & _
"is complete")
End Try
End Sub
Generowanie wyjątków
Sub Main()
Try
…
Throw New OverflowException()
…
Catch e As OverflowException
System.Console.WriteLine(e.Message)
…
End Try
End Sub