Create a callback that returns a value to its caller?
I am creating a class ("Class B") which is used by another class ("Class A") which instantiates it in VB.NET 3.5.
I would like Class B
to read a value assigned to one of its properties by Class A
every time an event happens in Class B
.
However, I would like this property value to be dynamically assigned to the property by Class A
when the event is fired in Class B
.
I was thinking about somehow creating a delegate function in Class A
that is linked to the property in Class B
so that when the event is fired in Class B
it would eventually invoke the delegate in Class A
so that it returns a value to Class B
.
I've been reading around delegates (that's how I arrived at the idea), but I am stu开发者_如何学编程ck on how to code this up - could someone please help with some skeleton code?
Also, I am not sure that this is the best approach for this - are there better solutions?
Thanks for your input.
I would probably isolate this from both classes a little. Maybe create a singleton that creates the value from Class A and just call that from Class B.
So
Public Class MySingleton
'Singleton related code here
Public Property MyDynamicValue As Integer
End Class
Then in Class A
Public Sub HandleClassBEvent(ByVal sender As Object, ByVal e As EventArgs) Handles ClassB.MyEvent
MySingleTonInstance.MyDynamicValue = SetMyDynamicValue()
End sub
In Class B
Public ReadOnly Property MyValueFromClassA Integer
Get
MySingletonInstance.MyDynamicValue
End Get
End Property
精彩评论