Results 1 to 6 of 6

Thread: CallBacks

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 1999
    Location
    Belfast
    Posts
    254

    CallBacks

    Has anyone implemented a callback mechanism in Vb 6? I have tried but have not been able to implement a solution. I have been using "Me" to send a reference to another object to invoke a reference but this fails.

    I would be very grateful for any feedback on this particularly whether it can be done or not. I have some simple source below to highlight the problem. you should get

    "object doesn;t support the method..........."

    Don't forget to set the instancing of IState to 2 ( not creatable ), this is acting as an interface.


    IState:
    --------
    Public Function talk() As Boolean

    End Function

    Public Function changeState(myContext As cls_Context) As Boolean

    End Function


    cls_State2
    -------------
    Implements IState

    Private Function IState_changeState(myContext As cls_Context) As Boolean
    myContext.setState (New cls_State1)
    End Function

    Private Function IState_talk() As Boolean

    MsgBox "I'm 2"

    End Function



    cls_State1
    -------------
    Implements IState

    Private Function IState_changeState(myContext As cls_Context) As Boolean
    myContext.setState (New cls_State2)
    End Function

    Private Function IState_talk() As Boolean
    MsgBox "I'm 1"
    End Function



    cls_Context
    ---------------
    Private myState As IState

    Public Function setState(myNewState As IState)
    myState = myNewState
    End Function

    Public Function Speak() As Boolean
    myState.talk
    End Function

    Private Sub Class_Initialize()
    Set myState = New cls_State1
    End Sub

    Public Sub AlterState()
    myState.changeState (Me)
    End Sub

    Lenin

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    That is all a bit confusing, what exactly are you trying to use callbacks with, a set of classes it looks like?

    Well one thing you need to use set in the setState function of cls_Context.

    You should check out the MSDN Coffee example among other things it has a good example of callbacks, at least I think that is the one.

  3. #3
    Helger
    Guest
    in the class:

    VB Code:
    1. Option Explicit
    2.  
    3. Public Event myCallbackEvent(ByVal myCallbackValue As Integer)
    4.  
    5. Sub EventTest()
    6.  
    7. RaiseEvent myCallbackEvent(19)
    8.  
    9. End Sub

    behind your form:

    VB Code:
    1. Option Explicit
    2. Dim WithEvents cls As Class1
    3.  
    4. Private Sub cls_myCallbackEvent(ByVal myCallbackValue As Integer)
    5.  
    6. Debug.Print myCallbackValue
    7.  
    8. End Sub
    9.  
    10. Private Sub Command1_Click()
    11.  
    12.  
    13. Set cls = New Class1
    14.  
    15. cls.EventTest
    16.  
    17. End Sub

    hth,

    Helger

  4. #4
    Helger
    Guest
    Also have a look at this thread if you should be interested in asynchronous communication. (The timer is needed because it is one of the few functions that gives control back to the calling thread right after it got triggered - just in case you should wonder).

    regards,

    Helger

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Feb 1999
    Location
    Belfast
    Posts
    254
    Helger thanks.

    Edneeis, yes I am try to incorporate callbacks with "a set of classes". I am trying to use callbacks to implement a behavioural patern ( the State Pattern ).

    I have been able to do this quite easily in Java, but had some issues in VB.

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    So did Helger fix your problem?

    Another thing that may help is making the methods the other object will call Friend instead of private.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width