Stuart McLachlan
stuart at lexacorp.com.pg
Wed Sep 21 17:18:24 CDT 2011
Read once, write many variable: Static Function mySafeGlobal(Optional invalue as Variant) as Variant Dim store as Variant If not isempty(store) then mySafeGlobal = store Exit Function End if If not ismissing(invalue) then store = invalue mySafeGlobal = store End if End Function -- Stuart On 21 Sep 2011 at 13:25, Kenneth Ismert wrote: > All, > > Ken Ismert: > > ... > > The question remains: is there any way to enforce safe global value > > modification in VBA, except by employing Gustav's discipline? > > > > Thinking about this further, I have come up with two approaches that > could work: > > 1. Use messaging. In this scenario, no global parameters are kept at > all. All objects communicate by sending messages to one another in a > standardized way. The sender is always identified. Receivers can then > decide who to ignore, and who to listen to. > > 2. Use a listener 'static class'. Some object requests to be the > 'speaker', first-come, first-served. Once accepted, only the speaker > can set parameters for the rest. Sample code: > > Option Explicit > > ' module MTestListener > Private Const ML_ERR_SPEAKER As Long = -2147213504 ' 8000 + > vbObjectError Private mlPtr As Long ' ObjPtr(Nothing) = 0 > > Public Sub ListenToMe(ByVal rSpeaker As Object) > If mlPtr = 0 Then > ' setting mlPtr does not increment the object's > ' reference count, so the object can close normally > mlPtr = ObjPtr(rSpeaker) > Else > Err.Raise ML_ERR_SPEAKER, "MTestListener.ListenToMe", _ > "Sorry, listening to Object at " & mlPtr > End If > End Sub > > Public Sub IgnoreMe(ByVal rSpeaker As Object) > If ObjPtr(rSpeaker) = mlPtr Then > mlPtr = 0 > End If > End Sub > > Public Sub SetParameter(ByVal rSpeaker As Object, sName As String, > vValue As Variant) > ' must match speaker, and not be nothing > If (ObjPtr(rSpeaker) = mlPtr) And (mlPtr <> 0) Then > ' set global parameter sName = vValue > Else > Err.Raise ML_ERR_SPEAKER, "MTestListener.SetParameter", _ > "Sorry, listening to Object at " & mlPtr > End If > End Sub > > Public Function GetParameter(sName As String) As Variant > ' anybody can get a parameter > End Function > -- > AccessD mailing list > AccessD at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/accessd > Website: http://www.databaseadvisors.com >