Using Static to improve performance

 

At times you will know the value you need comes from a function which you know during the running of the program the result will not change. An example would be VAT rate. Your queries may need this value again and again and it would be inefficient to have it looked up in a VAT table for each record being processed. The following structure will significantly improve performance in such cases as the value will only be determined once.


Function MyVar() As MyType
Static bInitialized As Boolean
Static lMyVar As MyType
    If bInitialized = False Then
        MyVar = ...
        bInitialized = True
    End If
    MyVar = lMyVar
End Function