Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Objects communicating with events

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PBWin/PBCC Objects communicating with events

    This is an example of using an event interface to distribute data to several other objects instances with one call. Like an intercom system; every object that is listening will get (notified of) the new data.

    Code:
    #COMPILE EXE
    
    ' Definitions of the interfaces
    
    INTERFACE NewDataEv AS EVENT
    ' Interface from the database to anyone interested
      INHERIT IUNKNOWN
      METHOD NameChanged (BYREF Into AS STRING)
    END INTERFACE
    
    INTERFACE StoreData
    ' Interface to the database to store data
      INHERIT IUNKNOWN
      METHOD DoReceiveANewName (BYREF NewName AS STRING)
    END INTERFACE
    
    ' This User interface class creates a name and sends it to its database
    ' Since it also displays the name it listens to the database when it
    ' declares it changed the name
    CLASS UserInterfaceClass AS EVENT
    
    INSTANCE DataStoreObj AS StoreData
    INSTANCE UI_Type      AS STRING
    INSTANCE NewName      AS STRING
    
    ' Implement the predefined interface 'NewDataEv' i.e. display the name
      INTERFACE NewDataEv AS EVENT
        INHERIT IUNKNOWN
        METHOD NameChanged (BYREF Into AS STRING)
          NewName$ = Into$
          MSGBOX UI_Type$ + "-> Name is now " + NewName$
        END METHOD
    
      END INTERFACE
    
    ' Implement the UI interface 'GetUserInput' i.e. read/get the name
      INTERFACE GetUserInput AS EVENT
        INHERIT IUNKNOWN
        METHOD AskForAName (BYVAL ANewName AS STRING)
          MSGBOX "A new name " + ANewName$ : DataStoreObj.DoReceiveANewName (ANewName$)
        END METHOD
    
        METHOD SetDataStore (BYVAL a AS StoreData) : DataStoreObj = a : END METHOD
        METHOD Set_UI_Type  (BYVAL a AS STRING)    : UI_Type$ = a     : END METHOD
      END INTERFACE
    
    END CLASS
    
    '-------------
    CLASS MyDataStore
    
      INTERFACE StoreData
        INHERIT IUNKNOWN
        METHOD DoReceiveANewName (BYREF NewName AS STRING)
          MSGBOX "A new name stored " + NewName$ : RAISEEVENT NewDataEv.NameChanged (NewName)
        END METHOD
      END INTERFACE
    
      EVENT SOURCE NewDataEv
    END CLASS
    
    
    
    FUNCTION PBMAIN()
      DIM oDataStore AS StoreData
      LET oDataStore = CLASS "MyDataStore"
    
    ' An UI object which reads and displays info
      DIM oInputPart      AS GetUserInput  : LET oInputPart      = CLASS "UserInterfaceClass"  ' Link a read name interface to a new UI object
      DIM oDisplayNameTxt AS NewDataEv     : LET oDisplayNameTxt = oInputPart                  ' And link a datastore interface to the UI object
      oInputPart.SetDataStore (oDataStore) : oInputPart.Set_UI_Type ("UI Input")               ' Tell the object to which datastore to send its changed data
    
    ' And another one, now called button
      DIM oNameButton      AS NewDataEv    : LET oNameButton      = CLASS "UserInterfaceClass"
      DIM oNameButtonInput AS GetUserInput : LET oNameButtonInput = oNameButton
      oNameButtonInput.SetDataStore (oDataStore) : oNameButtonInput.Set_UI_Type ("UI Button")
    
    ' Link the both UI objects to updates from the datastore, via their NewDataEv interface
      EVENTS FROM oDataStore CALL oDisplayNameTxt
      EVENTS FROM oDataStore CALL oNameButton
    
    ' Change the name via the first ui object (using the GetUserInput interface)
      oInputPart.AskForAName ("Montblanc")
    ' Then via the second, button, object
      oNameButtonInput.AskForAName ("Matterhorn")
    
      EVENTS END oDisplayNameTxt
      EVENTS END oNameButton
    
    END FUNCTION
    Enjoy, and pls feel free to notify me of any comments you might have
Working...
X