Announcement

Collapse
No announcement yet.

Using object events as a means to synchronise data

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

  • Rogier Knottenbelt
    replied
    Play time stopped ..... for a while ...

    Hi Michael,

    At long last some more time on my sleeve. I posted an example in the Source Code forum (http://www.powerbasic.com/support/pb...ad.php?t=42079)

    One note on the absence of any IN&OUTs:

    My programming background goes back to embedded programming. One of the 'tricks' used to speed up processing is to pass big chunks of data as a BYREF to a procedure, even if the data is not to be changed. This saves making a copy of the data every time the sub is called.
    In the docs I read that putting an IN statement before a BYREF does protect the original data from being changed. I.e. in the Method/End Method help section it reads:

    you may freely assign new values to BYVAL/IN parameters. They will simply be discarded when the METHOD exits

    I like the idea of indicating a BYREF parameter, which is meant to be a BYVAL, by putting an IN before it. I don't like it that I can then easily change it anyway and the changes will be discarded although its a BYREF...

    Too confusing for me so I choose not to use the IN&OUT syntax at this time


    Leave a comment:


  • Michael Mattias
    replied
    >I'll play around with the ins&outs of the byrefs, byvals, etc

    Point of call overrides?

    Point-of-call overrides (BYVAL, BYRE, BYCOPY ) are not something one "plays around with." And in procedure/method/property definitions, they are what they are, as documented.

    Just use the generated *.INC file or your own procedure/method/property header parameter definitions, eschewing point-of-call overrides and letting the compiler handle it until you fully understand the ramifications of doing so.

    As far as how you DEFINE your procedure/method/property parameters (BYVAL or BYREF), well, that's a function of your application.

    MCM

    Leave a comment:


  • Rogier Knottenbelt
    replied
    Thanks Michael

    The docs rule! I'll play around with the ins&outs of the byrefs, byvals, etc, just to make sure I get it all and will post the result (prob also in the source code forum)

    Leave a comment:


  • Michael Mattias
    replied
    I have no idea if this "works" but using PB/Win, 9.0.1 ...
    1. I changed all the 'typed' string variables from "$" suffix to AS STRING, as the doc says that's what METHOD wants
    2. I dropped all the "IN" in the parameter definitions as the doc does not allow for the inclusion of the word "IN" in those parameter descriptions.
    3. I dropped the BYVALs in the params (because I felt like it. I had no good reason).

    ....and the program compiles and executes.

    Code:
    ' test_method.bas
    
    
    INTERFACE NewDataEv AS EVENT
      INHERIT IUNKNOWN
      METHOD NameChanged (Into AS STRING)
     '  METHOD NameChanged (BYVAL Into AS STRING$)
    END INTERFACE
    
    INTERFACE StoreData
      INHERIT IUNKNOWN
      METHOD DoReceiveANewName (NewName AS STRING)
    END INTERFACE
    
    '--- Object defs
    CLASS UserInterfaceClass1 AS EVENT
    
    INSTANCE DataStoreObj AS StoreData
    
       INTERFACE NewDataEv AS EVENT
        INHERIT IUNKNOWN
        METHOD NameChanged (Into AS STRING)
          MSGBOX "1. Changed into " '+ Into$
        END METHOD
    
      END INTERFACE
    
      INTERFACE GetUserInput AS EVENT
        INHERIT IUNKNOWN
    
        METHOD SetDataStore (a AS StoreData)
          DataStoreObj = a
        END METHOD
    
        METHOD AskForAName
          MSGBOX "A new name created"
          DataStoreObj.DoReceiveANewName ("New Name")
        END METHOD
      END INTERFACE
    
    END CLASS
    
    '-------------
    CLASS UserInterfaceClass2 AS EVENT
    
    INSTANCE DataStoreObj AS StoreData
    
      INTERFACE GetUserInput AS EVENT
        INHERIT IUNKNOWN
    
        METHOD SetDataStore (a AS StoreData)
          DataStoreObj = a
        END METHOD
    
        METHOD AskForAName
          MSGBOX "A new name created"
          DataStoreObj.DoReceiveANewName ("New Name")
        END METHOD
      END INTERFACE
    
      INTERFACE NewDataEv AS EVENT
        INHERIT IUNKNOWN
        METHOD NameChanged (INTO AS STRING)
          MSGBOX "2. Changed into " '+ Into$
        END METHOD
      END INTERFACE
    
    END CLASS
    
    '-------------
    CLASS MyDataStore
    
      INTERFACE StoreData
        INHERIT IUNKNOWN
        METHOD DoReceiveANewName (NewName AS STRING)
          MSGBOX "A New Name..." + NewName$
          RAISEEVENT NewDataEv.NameChanged ("New name")
        END METHOD
      END INTERFACE
    
      EVENT SOURCE NewDataEv
    END CLASS
    
    
    
    FUNCTION PBMAIN()
      DIM oDataStore AS StoreData
      LET oDataStore = CLASS "MyDataStore"
    
    ' An UI object which reads input and displays it
      DIM oInputPart      AS GetUserInput : LET oInputPart      = CLASS "UserInterfaceClass1"  ' the input part of the UI object
      DIM oDisplayNameTxt AS NewDataEv    : LET oDisplayNameTxt = oInputPart                   ' the display part of the UI object
      oInputPart.SetDataStore (oDataStore)
    
    ' An UI object which displays changed data
      DIM oNameButton AS NewDataEv : LET oNameButton = CLASS "UserInterfaceClass2" ' Only display data
    
      EVENTS FROM oDataStore CALL oDisplayNameTxt
      EVENTS FROM oDataStore CALL oNameButton
      oInputPart.AskForAName
      EVENTS END oDisplayNameTxt
      EVENTS END oNameButton
    END FUNCTION

    Leave a comment:


  • Using object events as a means to synchronise data

    Im trying to get userinterface objects to communicate with a datastore using event objects. The datastore is receiving new data and distributing it to anyone interested.

    I can get all the inter object relations to work however, without any data transfer. I.e. I can create a notification that data is changed but I cant distribute the data with the notification. The code Im using is the following:

    Code:
    INTERFACE NewDataEv AS EVENT
      INHERIT IUNKNOWN
      METHOD NameChanged '(BYVAL Into$)
    END INTERFACE
    
    INTERFACE StoreData
      INHERIT IUNKNOWN
      METHOD DoReceiveANewName '(IN NewName$)
    END INTERFACE
    
    '--- Object defs
    CLASS UserInterfaceClass1 AS EVENT
    
    INSTANCE DataStoreObj AS StoreData
    
       INTERFACE NewDataEv AS EVENT
        INHERIT IUNKNOWN
        METHOD NameChanged '(BYVAL Into$)
          MSGBOX "1. Changed into " '+ Into$
        END METHOD
    
      END INTERFACE
    
      INTERFACE GetUserInput AS EVENT
        INHERIT IUNKNOWN
    
        METHOD SetDataStore (a AS StoreData)
          DataStoreObj = a
        END METHOD
    
        METHOD AskForAName
          MSGBOX "A new name created"
          DataStoreObj.DoReceiveANewName '("New Name")
        END METHOD
      END INTERFACE
    
    END CLASS
    
    '-------------
    CLASS UserInterfaceClass2 AS EVENT
    
    INSTANCE DataStoreObj AS StoreData
    
      INTERFACE GetUserInput AS EVENT
        INHERIT IUNKNOWN
    
        METHOD SetDataStore (a AS StoreData)
          DataStoreObj = a
        END METHOD
    
        METHOD AskForAName
          MSGBOX "A new name created"
          DataStoreObj.DoReceiveANewName '("New Name")
        END METHOD
      END INTERFACE
    
      INTERFACE NewDataEv AS EVENT
        INHERIT IUNKNOWN
        METHOD NameChanged '(BYVAL Into$)
          MSGBOX "2. Changed into " '+ Into$
        END METHOD
      END INTERFACE
    
    END CLASS
    
    '-------------
    CLASS MyDataStore
    
      INTERFACE StoreData
        INHERIT IUNKNOWN
        METHOD DoReceiveANewName '(in NewName$)
          MSGBOX "A New Name..." + NewName$
          RAISEEVENT NewDataEv.NameChanged '("New name")
        END METHOD
      END INTERFACE
    
      EVENT SOURCE NewDataEv
    END CLASS
    
    
    
    FUNCTION PBMAIN()
      DIM oDataStore AS StoreData
      LET oDataStore = CLASS "MyDataStore"
    
    ' An UI object which reads input and displays it
      DIM oInputPart      AS GetUserInput : LET oInputPart      = CLASS "UserInterfaceClass1"  ' the input part of the UI object
      DIM oDisplayNameTxt AS NewDataEv    : LET oDisplayNameTxt = oInputPart                   ' the display part of the UI object
      oInputPart.SetDataStore (oDataStore)
    
    ' An UI object which displays changed data
      DIM oNameButton AS NewDataEv : LET oNameButton = CLASS "UserInterfaceClass2" ' Only display data
    
      EVENTS FROM oDataStore CALL oDisplayNameTxt
      EVENTS FROM oDataStore CALL oNameButton
      oInputPart.AskForAName
      EVENTS END oDisplayNameTxt
      EVENTS END oNameButton
    END FUNCTION
    I try to transfer data with the notifications (as indicated by the out commented method parameters). However, powerbasic gives a compile error when I remove the comment signs (Error 481 Mismatch with prior definition). What am I doing wrong?

    If this data transfer is not possible can I determine where (i.e. which object instance) the notification came from so I can get the data from the notifier?


    Thanks in advance
    Rogier
Working...
X
😀
🥰
🤢
😎
😡
👍
👎