Kenneth Ismert
kismert at gmail.com
Wed Sep 21 13:25:07 CDT 2011
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