I often want an instance of a type to fire a message to an instance of another type at a certain moment.
I thought a Callback function could be used for that but I can't address a callback function within a type.
The example below is a completely useless piece of code but it illustrates what I want.
I have an instance of a TControl type.
This instance initialize an instance of a TProcess type. After a while the Process instance needs to tell the Control instance "I'm ready".
Ofcourse, I can make a reference to the Control instance within TProcess but then I need to know it's type when calling it. Like I show in the second example. But that requires me to make one generic type and extend all my types from that type. That's not what I prefer to do.
What's best?
Second example (use a generic type TElement):
I thought a Callback function could be used for that but I can't address a callback function within a type.
The example below is a completely useless piece of code but it illustrates what I want.
I have an instance of a TControl type.
This instance initialize an instance of a TProcess type. After a while the Process instance needs to tell the Control instance "I'm ready".
Ofcourse, I can make a reference to the Control instance within TProcess but then I need to know it's type when calling it. Like I show in the second example. But that requires me to make one generic type and extend all my types from that type. That's not what I prefer to do.
What's best?
SuperStrict Global o:TControl = TControl.Create() o.Start(500) While Not KeyHit(KEY_ESCAPE) o.Update() Wend End Function StopIt() Print "Stop" o.Stop() End Function Type TProcess Field piStart% Field piDuration% Field poCallback() Function Create:TProcess(iDuration%, oCallback()) Local o:TProcess = New TProcess o.piDuration = iDuration o.poCallback = oCallback o.piStart = MilliSecs() Return o End Function Method Update() Print "Busy" If MilliSecs()-piStart>=piDuration Then poCallback() End Method End Type Type TControl Field poProcess:TProcess Function Create:TControl() Local o:TControl = New TControl Return o End Function Method Start(iDuration%) Print "Start process..." 'This does not work, but it is what I actually want... poProcess = TProcess.Create(iDuration, Self.Stop) 'however this works, but that is Not what I want. ' poProcess = TProcess.Create(iDuration, StopIt) 'uncomment this line to see what happens if you run this code. End Method Method Update() If poProcess Then poProcess.Update() End Method Method Stop() Print "Process done." poProcess = Null End Method End Type
Second example (use a generic type TElement):
SuperStrict Global o:TControl = TControl.Create() o.Start(500) While Not KeyHit(KEY_ESCAPE) o.Update() Wend End Type TProcess Field piStart% Field piDuration% Field poControl:TElement Function Create:TProcess(iDuration%, oControl:TElement) Local o:TProcess = New TProcess o.piDuration = iDuration o.poControl = oControl o.piStart = MilliSecs() Return o End Function Method Update() Print "Busy" If MilliSecs()-piStart>=piDuration Then poControl.Callback() End Method End Type Type TElement Method Callback() Abstract End Type Type TControl Extends TElement Field poProcess:TProcess Function Create:TControl() Local o:TControl = New TControl Return o End Function Method Start(iDuration%) Print "Start process..." poProcess = TProcess.Create(iDuration, Self) End Method Method Update() If poProcess Then poProcess.Update() End Method Method Callback() Print "Process done." poProcess = Null End Method End Type