RaiseEvents with Functions

Kesher

Member
Joined
Feb 12, 2007
Messages
6
Programming Experience
10+
Hi everyone,

Is there any way to create an Event in a class so that the host application can return data?

i.e. Function instead of Sub.

Example in Host Application:
Private Function class_event() as Integer Handles class.event
class_event=4
End Sub

Example in Class:
return = RaiseEvent(BindRequest("", "", 0, 0))

Hope that makes sense.
Can anyone suggest a way of doing this?

Many thanks
Dan Journo
www.TextOver.com
 
I think you have to explain better what you want to do.

Perhaps you need event to return data? something like this:
VB.NET:
Public Event happens(ByVal i As Integer)
VB.NET:
RaiseEvent happens(4)
VB.NET:
Sub handler(ByVal i As Integer) Handles instance.happens
   'event passed i (value 4 in this example)
End Sub
 
Nope, it needs to be the other way round. Instead of passing data with the event, i need to be able to pass data back to the class.

An example would be the Form.Closing event.
VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] frmMain_Closing1([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.ComponentModel.CancelEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Closing[/SIZE]
[SIZE=2]e.Cancel = true[/SIZE]
[SIZE=2]End Sub
[/SIZE]

The form that raised that event waits to find out what e.Cancel is set to before continuing with the closing. I want my class to be able to raise an event and then wait for the event's sub to complete, and read a variable which has been set, just like the above example.
 
Last edited:
Here is two examples doing that, for value type parameter it have to be declared ByRef to pass data back to the event raiser, for reference type parameter you don't have to use ByRef because the parameter instance is already a reference. (the difference in latter case is that a new variable is allocated to hold reference to the same object instance) In both cases the event raiser can read back the parameter value after the event is raised, since the event handlers operate synchronous, not until all event handler methods are finished is the code line after RaiseEvent invoked.

Example code, run method test_testevent to test.
VB.NET:
    Public Class reference 'just a reference class
        Public data As Boolean
    End Class
 
    Public Class testevent 'a class that raises events
        Public Event happens(ByRef b As Boolean) 'notice ByRef, Boolean is a value type variable
        Public Event happens2(ByVal d As reference) 'notice ByVal, reference is a reference type variable
 
        Public Sub invokes()
            Dim b As Boolean = True
            RaiseEvent happens(b)
            MsgBox(b.ToString) 'see if b value is changed
 
            Dim d As New reference
            d.data = True
            RaiseEvent happens2(d)
            MsgBox(d.data.ToString) 'see if d.data value is changed
        End Sub
    End Class
 
    Private WithEvents te As New testevent
    Private Sub te_happens(ByRef b As Boolean) Handles te.happens
        b = False
    End Sub
    Private Sub te_happens2(ByVal d As reference) Handles te.happens2
        d.data = False
    End Sub
 
    Private Sub test_testevent()
        te.invokes()
    End Sub
 
Back
Top