return a value within an event

S

Sam

Hi,
How, if possible, can I return a value from an event ?
In :

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSave.Click

btnSave_Click and btnSave.Click must have the same signature so I can't
figure out how to do it.

Any help ?

Thx
 
J

JohnFol

But these are not return values, they are parameters passed to the event. If
you generate your own event you can pass parameters to be handled.

ie
in a class called file processor I add in my event

Public Event TerminalFailure(ByVal strFailureDescription As String)

Then, when I make use of the class I declare it with events.
Private WithEvents m_FileProcessor As FileProcessor

Now within the code I get the signature as follows:

Private Sub EngineFailure(ByVal strFailureReason As String) Handles
m_FileProcessor.TerminalFailure
 
S

Sam

Yes I know they are parameters, what I was wondering is how to do
something like:

Private Function btnSave_Click(ByVal sender As System.Object, ByVal e
As
System.EventArgs) Handles btnSave.Click as Boolean

Of course this syntax is not correct but how can I make it work ?

Thx
 
C

Cor Ligthert

Sam,

You get probably a lot answers on this,

I would do thise case
\\\
BtnSave_Click(sender, nothing)
///
You pass the original sender and the event you cannot pass because it is not
equal.

Because I am not sure if this is completly answers your question, you can
than, when you have not used that already, the Tag for passing a value
first.

sender.tag = myvalue

I hope this helps,

Cor
 
S

Sam

Thx.
I think I'll just use a member variable to my class to keep track if
the event succeeded or not (Saving stuff).
 
H

Herfried K. Wagner [MVP]

Sam said:
Private Function btnSave_Click(ByVal sender As System.Object, ByVal e
As
System.EventArgs) Handles btnSave.Click as Boolean

Of course this syntax is not correct but how can I make it work ?

Events cannot return values like function procedures. However, you if you
write your own events you can return values to the object raising the event
using custom event arguments:

\\\
Public Event Foo(ByVal sender As Object, ByVal e As FooEventArgs)
..
..
..
Protected Sub OnFoo(ByVal e As FooEventArgs)
RaiseEvent Foo(Me, e)
End Sub
..
..
..
Dim e As New FooEventArgs()
OnFoo(e)
If e.Handled Then
...
Else
...
End If
..
..
..
Private Sub Bla_Foo(ByVal sender As Object, ByVal e As FooEventArgs)
If...Then
e.Handled = True
End If
End Sub
..
..
..
Public Class FooEventArgs
Inherits EventArgs

Private m_Handled As Boolean

Public Property Handled() As Boolean
Get
Return m_Handled
End Get
Set(ByVal Value As Boolean)
m_Handled = Value
End Set
End Property
End Class
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top