|
-
Mar 4th, 2002, 05:05 PM
#1
Thread Starter
Addicted Member
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
-
Mar 4th, 2002, 06:17 PM
#2
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.
-
Mar 4th, 2002, 06:24 PM
#3
in the class:
VB Code:
Option Explicit
Public Event myCallbackEvent(ByVal myCallbackValue As Integer)
Sub EventTest()
RaiseEvent myCallbackEvent(19)
End Sub
behind your form:
VB Code:
Option Explicit
Dim WithEvents cls As Class1
Private Sub cls_myCallbackEvent(ByVal myCallbackValue As Integer)
Debug.Print myCallbackValue
End Sub
Private Sub Command1_Click()
Set cls = New Class1
cls.EventTest
End Sub
hth,
Helger
-
Mar 4th, 2002, 06:31 PM
#4
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
-
Mar 5th, 2002, 04:12 AM
#5
Thread Starter
Addicted Member
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.
-
Mar 5th, 2002, 04:14 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|