UtterAccess HomeUtterAccess Wiki

Welcome Guest ( Log In | Register )

Custom Search
Edit Discussion
> FunctionLibrary:Self-Healing Object Variables    

A common scenario is that we may want to have an object variable that we need to be able to refer to from several different parts in our application. It's tempting to just use a public variable but this is not without its problems. For one, we can't be certain it's ready to use, hasn't been closed or otherwise dropped out of scope. Thus a good technique is to write a Property procedure as thus:

Property Get myConn() As ADODB.Connection

'This is our real reference variable -
'Note it's declared as static so it will
'always be available between calls
Static cn As ADODB.Connection

'Verify it's alive - We use a Select
'to enable lazy evaluation and avoid
'an error if we used If/Then
Select Case True
 Case cn Is Nothing, cn.State = adStateClosed
   Set cn = New ADODB.Connection
   With cn
      <set various parameters...>
   End With
End Select

'Now it's ready for re-use!
Set myConn = cn

End Property

By using the technique we free ourselves of worrying and checking whether it's been initialized. The technique is not restricted to ADO and can be used for other libraries.

Edit Discussion
Custom Search

Thank you for your support!
This page has been accessed 1,431 times.  This page was last modified 21:15, 25 July 2010 by BananaRepublic.   Disclaimers