Raising Event

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have an event declared in class1

Now i want to raise the event in the my class2 or anywhere other than class1

Is it possible? If so how

Thank you
 
Santhoshi said:
I have an event declared in class1.

Now i want to raise the event in the my class2 or anywhere other than
class1.

Is it possible? If so how?

Basically you will have to declare a method on class1 that can raise the
event.
 
Cor said:
Hi Santhosi,

You have to declare your event "public"

I hope this helps?

Public is actually irrelevent. The event keyword(in C# anyway, don't see VB
being that different) is specifically used over a plain delegate type
because it restricts outside classes from raising or clearing the event,
otherwise a plain delegate would suffice. This creates an event in the
assembly, this event has two required methods (add and remove in C#, .addon
and .removeon in IL) that performs the addition and removal of a delegate
from the event. Even when the event is declared as a field underneath it is
a property with a backing delegate field(compiler handles that). Because of
this, an event type *never* directly provides access to the backing
delegate, in fact code has no way to actually get to the backing delegate
because of the nature of events.

Now, there is a raise accessor defined by the CLI spec, specifically .fire
in IL, but the recommendation is for that accessor to be protected and it
isn't surfaced in C# and VB(AFAIK). This accessor doesn't have a restriction
that it *must* be protected, to my knowledge, and if you use a language that
allows public raise accessors, it will be possible to raise the event from
another class, otherwise you are basically out of luck(note, C# doesn't
consume raise accessors in my experience).

It really is simplier to simply write a method to do what you want(thats
basically all a raise accessor is, after all).
 
Hi Daniel,

Can you try this (needs only to open the ide, start a VB project and paste
it in, and set the start in your properties to sub main), and then give
comments, I did it in VB, because there was my answer based on.

This was the question
I have an event declared in class1.
Now i want to raise the event in the my class2 or anywhere other than
class1.
Is it possible? If so how?
This was my answer
You have to declare your event "public"

I am curious to your answer

Cor

This is my sample.
\\\
Public Class class1
Public Event Hello()
Public Sub sayhello()
RaiseEvent Hello()
End Sub
End Clas
Public Module class2
Private WithEvents myhi As New class1
Public Sub Main()
myhi.sayhello()
End Sub
Private Sub hihi() Handles myhi.Hello
MessageBox.Show("Hello Daniel")
End Sub
End Module
///
 
Cor said:
Can you try this (needs only to open the ide, start a VB project and paste
it in, and set the start in your properties to sub main), and then give
comments, I did it in VB, because there was my answer based on.

This was the question
I have an event declared in class1.
Now i want to raise the event in the my class2 or anywhere other than
class1.
Is it possible? If so how?
This was my answer
You have to declare your event "public"

You don't have to declare the event public in order to *raise* the
event. You have to declare the event public in order to *subscribe* to
(or unsubscribe from) it.

The reason your code allows other classes to raise the events is
because you've *also* given a sayhello method. It would be possible to
give that method without the Hello event being public, in which case
only the class could subscribe to the event, but anyone could raise it
(indirectly, via the method call).
 
Back
Top