Declaring array withevents


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Declaring array withevents

  1. #1
    Piyush Guest

    Declaring array withevents

    Hi Folks,

    I would like to have a way to declare array of objects "withevent". VB
    itself doesnt allows defining array of objects "withevent", can anybody
    suggest a way ?

    Thanks,

    Piyush Gupta.


  2. #2
    Anthony Jones Guest

    Re: Declaring array withevents

    Use a mediator pattern.

    The basic concept in this solution is to have single instance of an event
    raising class which also implements an interface that has a matching set
    of methods that raise the events.

    All the receiver needs is a WithEvents reference to the instance of the
    Mediator class. All the senders need to do is hold a reference to the
    mediator class and call the appropriate method for the event it wishes to
    raise.

    The mediator thus allows any number of receivers (or just one) to receive
    events from any number of senders.

    Example:-

    === Code ===
    'Mediator Class
    'If the source of the event is unimportant drop the parameter
    Event Changed(Item as Child)

    Public Sub Changed(Item as Child)
    RaiseEvent Item
    End Sub

    '-------
    'Parent Class

    Private mcol as Collection
    Private WithEvents moMediator as Mediator

    'Code when adding child somewhere
    mcol.Add oChild, oChild.Name (or whatever)
    Set oChild.Mediator = moMediator

    Private Sub moMediator_Changed(Item as Child)
    'Do Stuff
    End Sub

    '-------
    'Child Class

    Private moMediator as Mediator

    Friend Property Set Mediator(ByVal voMediator as Mediator)
    Set moMediator = voMediator
    End Property

    Public Property Let Name (rsName as String)

    msName = rsName
    moMediator.Changed Me

    End Property

    === End Code ===


    --
    Anthony Jones
    Nuesoft Ltd



  3. #3
    Piyush Guest

    Re: Declaring array withevents

    Thanks Anthony, it was a real good solution to the problem.

    Anthony Jones wrote:

    > Use a mediator pattern.
    >
    > The basic concept in this solution is to have single instance of an event
    > raising class which also implements an interface that has a matching set
    > of methods that raise the events.
    >
    > All the receiver needs is a WithEvents reference to the instance of the
    > Mediator class. All the senders need to do is hold a reference to the
    > mediator class and call the appropriate method for the event it wishes to
    > raise.
    >
    > The mediator thus allows any number of receivers (or just one) to receive
    > events from any number of senders.
    >
    > Example:-
    >
    > === Code ===
    > 'Mediator Class
    > 'If the source of the event is unimportant drop the parameter
    > Event Changed(Item as Child)
    >
    > Public Sub Changed(Item as Child)
    > RaiseEvent Item
    > End Sub
    >
    > '-------
    > 'Parent Class
    >
    > Private mcol as Collection
    > Private WithEvents moMediator as Mediator
    >
    > 'Code when adding child somewhere
    > mcol.Add oChild, oChild.Name (or whatever)
    > Set oChild.Mediator = moMediator
    >
    > Private Sub moMediator_Changed(Item as Child)
    > 'Do Stuff
    > End Sub
    >
    > '-------
    > 'Child Class
    >
    > Private moMediator as Mediator
    >
    > Friend Property Set Mediator(ByVal voMediator as Mediator)
    > Set moMediator = voMediator
    > End Property
    >
    > Public Property Let Name (rsName as String)
    >
    > msName = rsName
    > moMediator.Changed Me
    >
    > End Property
    >
    > === End Code ===
    >
    >
    > --
    > Anthony Jones
    > Nuesoft Ltd
    >
    >
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center