Re: RaiseEvent from an item to collection

Tech-Archive recommends: Fix windows errors by optimizing your registry



thanks ken, good stuff.

> Protected? Wazzat?
my old powerbuilder days coming back...

"Ken Halter" <Ken_Halter@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:OIkw8nMfFHA.2512@xxxxxxxxxxxxxxxxxxxxxxx
> "Craig Buchanan" <msnews.microsoft.com> wrote in message
> news:esv%236$LfFHA.3940@xxxxxxxxxxxxxxxxxxxxxxx
> > Karl-
> >
> > How would a callback work in this case? would the instance call a
> > protected
> > method on the collection class?
> >
> > Thanks,
> >
> > Craig
>
> Protected? Wazzat?
>
> If you start a new project and add 3 classes called clsCollection, clsItem
> and IChanged, here's a way to get change notifications. Paste, run, watch
> the immediate window whne you click the button.
> '===========================Form Code (needs a command button)
> Option Explicit
>
> 'If you want the form to know, need WithEvents....
> Private WithEvents mobjColl As clsCollection
>
> Private Sub Command1_Click()
> Dim oItem As clsItem
> Dim i As Integer
>
> Set mobjColl = New clsCollection
>
> 'Set up a few items
> For i = 1 To 5
> Set oItem = New clsItem
> oItem.TheData = "This is test #" & i
> mobjColl.Add oItem, "Key " & i
> Next
>
> 'Now, change the data and watch the debug window
> For i = 1 To 5
> Set oItem = mobjColl.Item("Key " & i)
> oItem.TheData = "This is element #" & i
> Next
>
> Set mobjColl = Nothing
>
> End Sub
>
> Private Sub mobjColl_StuffChanged(ByVal Key As String _
> , ByVal OldData As String, ByVal NewData As String)
>
> 'If you want the form to know....
> Debug.Print "mobjColl_StuffChanged", Key, OldData, NewData, Timer
> End Sub
> '===========================Class IChanged code
> Option Explicit
>
> Public Sub DataChanged(ByVal Key As String _
> , ByVal OldData As String, ByVal NewData As String)
> '
> End Sub
> '===========================Class clsItem code
> Option Explicit
>
> Private msKey As String
> Private msTheData As String
> Private mobjInterface As IChanged
>
> Friend Property Get TheInterface() As IChanged
> Set TheInterface = mobjInterface
> End Property
>
> Friend Property Set TheInterface(ByVal Setting As IChanged)
> Set mobjInterface = Setting
> End Property
>
> Friend Property Get Key() As String
> Key = msKey
> End Property
>
> Friend Property Let Key(ByVal Setting As String)
> msKey = Setting
> End Property
>
> Public Property Get TheData() As String
> TheData = msTheData
> End Property
>
> Public Property Let TheData(ByVal Setting As String)
> Dim sOldStuff As String
> sOldStuff = msTheData
> If Not mobjInterface Is Nothing Then
> Call mobjInterface.DataChanged(msKey, sOldStuff, Setting)
> End If
> msTheData = Setting
> End Property
> '===========================Class clsCollection code
> Option Explicit
>
> Private mobjColl As Collection
>
> Public Event StuffChanged(ByVal Key As String _
> , ByVal OldData As String, ByVal NewData As String)
>
> Implements IChanged
>
> Public Sub Add(Item As clsItem, Key As String)
> Set Item.TheInterface = Me
> Item.Key = Key
> mobjColl.Add Item, Item.Key
> End Sub
>
> Public Function Item(Key As String) As clsItem
> Set Item = mobjColl(Key)
> End Function
>
> Private Sub Class_Initialize()
> Set mobjColl = New Collection
> End Sub
>
> Private Sub IChanged_DataChanged(ByVal Key As String _
> , ByVal OldData As String, ByVal NewData As String)
> 'If you want this class to know....
> Debug.Print "IChanged_DataChanged", Key, OldData, NewData, Timer
>
> 'If you want to raise an event
> RaiseEvent StuffChanged(Key, OldData, NewData)
> End Sub
> '===========================
>
> --
> Ken Halter - MS-MVP-VB - http://www.vbsight.com
> DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
> Please keep all discussions in the groups..
>
>


.


Quantcast