To declare a static variable, type the Static keyword on the left of the Dim keyword. You should always initialize a static variable before using it. To make the local variables of our Starter() function static, we can declare them as follows: Public Module Exercise Private Sub Starter(ByVal y As Integer) Static Dim a As Double = 112.5 Static Dim b As Double = 175.25 Dim Result As String a = a / y b = b + 2 Result = "y = " & vbTab & CStr(y) & vbCrLf & _ "a = " & vbTab & CStr(a) & vbCrLf & _ "b = " & vbTab & CStr(b) & vbCrLf & _ "b/a = " & vbTab & CStr(b / a) MsgBox(Result) End Sub Public Function Main() As Integer Starter(2) Starter(2) Starter(2) Starter(2) Return 0 End Function End Module This time, when executing the program, it would produce: Notice that, this time, each local variable keeps its newly changed value when the function exits. |
|
|||
|