Podstawy programowania 3 dr Jerzy Bartoszek jbartoszekwskiz edu
Podstawy programowania (3) dr Jerzy Bartoszek jbartoszek@wskiz. edu jerzy. bartoszek@put. poznan. pl
Przykład procedury Module 1 Sub Main() Display. Message("Hello from Visual Basic") End Sub Display. Message(By. Val str. Text As String) System. Console. Write. Line(str. Text) End Sub End Module
Schemat definiowania procedury Sub name [(arglist)] [ statements ] [ Exit Sub ] [ statements ] End Sub
Przykład funkcji Module 1 Sub Main() Dim int. Value As Integer = 2 System. Console. Write. Line("{0}+{1}={2}", int. Value, _ int. Value, Add(int. Value, int. Value)) End Sub Function Add(By. Val i 1 As Integer, By. Val i 2 As Integer)_ As Long Return i 1 + i 2 End Function End Module
Schemat definiowania funkcji Function name [(arglist)] As type [ statements ] [ Exit Function ] [ statements ] End Function Musi zawierać return expr.
Przekazywanie parametrów • By. Val - przez wartość. Stosowany dla parametrów „wejściowych”. Do procedury/funkcji przekazywana jest wartość parametru aktualnego. • By. Ref – 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 Test. Func(By. Val x As Integer, By. Ref y As _ Integer, By. Ref 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 Test. Func(a, b, c) Console. Write. Line("{0} {1} {2}", a, b, c) ' 1 2 5
Wartości opcjonalne parametrów Module 1 Sub Main() Display. Message() End Sub Display. Message(Optional By. Val str. Text _ As String = "Hello from Visual Basic") System. Console. Write. Line(str. Text) End Sub End Module
Przykład parametru opcjonalnego ' Optional parameters must be listed last and must have a default value Sub Say. Hello(By. Val name As String, _ Optional By. Val prefix As String = "") Console. Write. Line("Greetings, " & prefix & " " & name) End Sub Say. Hello("Nowak", "Dr. ") Say. Hello("Kowalski")
Zmienna liczba parametrów Module 1 Sub Main() Display. Message("First message: ", "Hi" ) Display. Message("Second message: ", "Hello", "there" ) Dim Text. Array() As String = {"Hello", "from", "Visual", _ "Basic"} Display. Message("Third message: ", Text. Array ) End Sub Display. Message(By. Val Title As String, By. Val Param. Array _ Message. Text() As String) Dim int. Loop. Index As Integer System. Console. Write. Line(Title) For int. Loop. Index = 0 To UBound(Message. Text) System. Console. Write. Line(Message. Text(int. Loop. Index)) Next int. Loop. Index End Sub End Module
Zmienne statyczne Module 1 Sub Main() Dim int. Loop. Index As Integer, int. Value = 0 For int. Loop. Index = 0 To 4 int. Value = Counter() Next int. Loop. Index System. Console. Write. Line(int. Value) End Sub Function Counter() As Integer Dim int. Count. Value As Integer int. Count. Value += 1 Return int. Count. Value End Function End Module
Przykład zmiennej liczby parametrów Function Sum(By. Val Param. Array 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 1 Sub Main() Dim int. Loop. Index As Integer, int. Value = 0 For int. Loop. Index = 0 To 4 int. Value = Counter() Next int. Loop. Index System. Console. Write. Line(int. Value) End Sub Function Counter() As Integer Static int. Count. Value As Integer int. Count. Value += 1 Return int. Count. Value 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 [ try. Statements ] [Catch [ exception 1 [ As type 1 ] ] [ When expression 1 ] catch. Statements 1 [Exit Try] ] [Catch [ exception 2 [ As type 2 ] ] [When expression 2 ] catch. Statements 2 [ Exit Try ] ] … [Catch [ exceptionn [ As typen ] ] [ When expressionn ] catch. Statementsn ] [ Exit Try ] ] [ Finally [ finally. Statements ] End Try
Przykład obsługi wyjątków Sub Main() Dim int 3 As Integer Try int 3 = … System. Console. Write. Line("The answer is {0}", int 3) Catch e As System. Argument. Out. Of. Range. Exception System. Console. Write. Line("Exception: Argument out of" _ & " range!") Catch e As System. Argument. Exception System. Console. Write. Line("Exception: Invalid argument„ _ & " value!") Catch e As Exception System. Console. Write. Line("Exception occurred!") End Try End Sub
Przykład z when Sub Main() Dim int 1 = 0, int 2 = 1, int 3 As Integer Try int 3 = int 2 / int 1 System. Console. Write. Line( _ "The answer is {0}", int 3) Catch When Err. Number = 6 System. Console. Write. Line( _ "Exception: Arithmetic overflow!") End Try End Sub
Przykład z finally Sub Main() … Try … Catch e As System. Argument. Out. Of. Range. Exception System. Console. Write. Line("Exception: Argument out of" _ & " range!") … Catch e As Exception System. Console. Write. Line("Exception occurred!") Finally System. Console. Write. Line("Execution of sensitive code " & _ "is complete") End Try End Sub
Generowanie wyjątków Sub Main() Try … Throw New Overflow. Exception() … Catch e As Overflow. Exception System. Console. Write. Line(e. Message) … End Try End Sub
- Slides: 19