events used in .NET framework


T

Tony Johansson

Hi!

Here I have two classes(Control and Button) that show how it might look like
in the .NET.
I have also a user defined class called MyExample that is using the Button
class.
I'm almost sure that only the class that contain the event can raise this
same event.
I have read this in a book so I hoped it's true ? So in this case only the
Control class can raise the Click event even if an object of a Button class
is used and the Click event is used.
In my user defined class instansiating a Button object and then use the
Click event so the event handler
ok_Click can be called when the user click on the button.

What I want to know is if anybody could give some example how the .NET
framework might look like to be able to raise
an event whan a Button object is using the Click event that is located in
the Control class ?

namespece System.Windows.Forms
{
public class Control
{
public event EventHandler Click;
. . .
}

public class Button : Control
{
...
}
}

class MyExample
{
private System.Windows.Forms.Button ok:

public MyExample()
{
ok.Click += new System.EventHandler(ok_Click);
}

private void ok_Click(object sender , System.EventArgs e)
{
. . .
}
}

//Tony
 
Ad

Advertisements

P

Peter Duniho

Tony said:
Hi!

Here I have two classes(Control and Button) that show how it might look like
in the .NET.
I have also a user defined class called MyExample that is using the Button
class.
I'm almost sure that only the class that contain the event can raise this
same event.
I have read this in a book so I hoped it's true ? [...]

It is only true to an extent. In particular, when using the default
"field-like" events, only the class declaring the event has access to
the field storing the event's delegate.

So, no code outside that class can view the variable and use it. But,
even in that case the class declaring the event can provide a mechanism
for raising the event, and of course with an explicitly-declared event,
the class can provide any kind of access at all.

For examples of exceptions exactly of the nature I describe above, see
methods such as Control.OnClick(), Control.InvokeOnClick(), and
Button.PerformClick(). The first two are protected, given control of
the event to sub-classes of the Control class, and the latter is public,
allowing any code at all to cause a Click event to be raised (but in the
Button class specifically).
[...]
What I want to know is if anybody could give some example how the .NET
framework might look like to be able to raise
an event whan a Button object is using the Click event that is located in
the Control class ?

I don't know what that even means. If you subscribe to the Click event
of a Button, you can in the event handler raise whatever other event you
like, as long as you have a way to raise it of course.

Pete
 
T

Tony Johansson

Peter Duniho said:
Tony said:
Hi!

Here I have two classes(Control and Button) that show how it might look
like in the .NET.
I have also a user defined class called MyExample that is using the
Button class.
I'm almost sure that only the class that contain the event can raise this
same event.
I have read this in a book so I hoped it's true ? [...]

It is only true to an extent. In particular, when using the default
"field-like" events, only the class declaring the event has access to the
field storing the event's delegate.

So, no code outside that class can view the variable and use it. But,
even in that case the class declaring the event can provide a mechanism
for raising the event, and of course with an explicitly-declared event,
the class can provide any kind of access at all.

For examples of exceptions exactly of the nature I describe above, see
methods such as Control.OnClick(), Control.InvokeOnClick(), and
Button.PerformClick(). The first two are protected, given control of the
event to sub-classes of the Control class, and the latter is public,
allowing any code at all to cause a Click event to be raised (but in the
Button class specifically).
[...]
What I want to know is if anybody could give some example how the .NET
framework might look like to be able to raise
an event whan a Button object is using the Click event that is located in
the Control class ?

I don't know what that even means. If you subscribe to the Click event of
a Button, you can in the event handler raise whatever other event you
like, as long as you have a way to raise it of course.

Pete

What I mean is if I subscribe to the Click event in class Button which is
derived from Control.
then I assume that the actual raise of the Click event is done in the
Control class ?
is that correct understood ?

//Tony
 
P

Peter Duniho

Tony said:
What I mean is if I subscribe to the Click event in class Button which is
derived from Control.
then I assume that the actual raise of the Click event is done in the
Control class ?
is that correct understood ?

It all depends on how you define "the actual raise of the Click event".
But no matter how you define it, it's _possible_ for a class to meet
the definition in code outside the class, unless your definition
specifically prohibits that (in which case, it's not an interesting
question to answer).

For example, "the actual raise of the Click event" could mean "code
taking the initial steps to explicitly cause the Click event to be
raised". In that case, something as simple as the Button.PerformClick()
method qualifies, and of course that can be called from outside the class.

So, how about this definition instead: "the direct invocation of the
delegate instance representing the subscribers to the event"? Sounds
good. Except that the class declaring the event can, if it wants,
expose that delegate reference to the public. In which case, code
outside the class can, yet again, perform "the actual raise of the Click
event".

The fact is, the vast majority of examples of events – probably over 99%
of the time – the only type that can access the event field and raise
the event _directly_ by invoking the delegate is the class where the
event is declared.

But, there's no absolute statement that can be made about the question.
If you are going to ask the question, you need to be willing to accept
that.

Pete
 
T

Tony Johansson

Peter Duniho said:
It all depends on how you define "the actual raise of the Click event".
But no matter how you define it, it's _possible_ for a class to meet the
definition in code outside the class, unless your definition specifically
prohibits that (in which case, it's not an interesting question to
answer).

For example, "the actual raise of the Click event" could mean "code taking
the initial steps to explicitly cause the Click event to be raised". In
that case, something as simple as the Button.PerformClick() method
qualifies, and of course that can be called from outside the class.

So, how about this definition instead: "the direct invocation of the
delegate instance representing the subscribers to the event"? Sounds
good. Except that the class declaring the event can, if it wants, expose
that delegate reference to the public. In which case, code outside the
class can, yet again, perform "the actual raise of the Click event".

The fact is, the vast majority of examples of events - probably over 99%
of the time - the only type that can access the event field and raise the
event _directly_ by invoking the delegate is the class where the event is
declared.

But, there's no absolute statement that can be made about the question. If
you are going to ask the question, you need to be willing to accept that.

Pete

Assume you have a class called Control as in the .NET Framework.
In this class we have have declared an event.
event EventHandler Click
What I want to say is if for example Button want to raise the Click event.
Where is the statement located that does the actual
raise of the event ? I mean something like Click(this, new EventArg()) As
far as I understand this statement Click(this, new EventArg()) must be
located in the Control class but some other derived class can call method in
this Control class where this
Click(this, wew EventArg()) is located so a raise can be done.

Hope you understand what I mean.

//Tony
 
P

Peter Duniho

Tony said:
Assume you have a class called Control as in the .NET Framework.
In this class we have have declared an event.
event EventHandler Click
What I want to say is if for example Button want to raise the Click event.
Where is the statement located that does the actual
raise of the event ? I mean something like Click(this, new EventArg()) As
far as I understand this statement Click(this, new EventArg()) must be
located in the Control class but some other derived class can call method in
this Control class where this
Click(this, wew EventArg()) is located so a raise can be done.

Hope you understand what I mean.

I do understand what you mean. But you don't appear to understand what
I mean.

Assuming this code:

Action action1 = () => Console.WriteLine("event");
Action action2 = action1;

…what significant difference is there between this:

action1();

…and this:

action2();

?

Answer: none. The name of the variable used to cause the delegate
instance to be invoked is irrelevant. What's important is that it was
invoked.

So, assuming this code:

class Test
{
public event Action TestEvent;

public Action TestEventAction { get { return TestEvent; } }

void RaiseTestEvent()
{
// Raising inside class:
TestEvent();
}
}

class Outside
{
void Method()
{
Test test = new Test();

test.TestEvent += () => Console.WriteLine("event");

// Raising outside class:
test.TestEventAction();
}
}

…there is also no difference between the code commented "// Raising
inside class" and the code commented "// Raising outside class".

Most classes aren't designed this way, and of the remaining few that
are, almost certainly they should not have been either. But the fact
that it _can_ happen means that the answer to your question is
basically, no…to raise the event does _not_ require the code doing that
to be inside the class where the event is declared. That's true no
matter what non-trivial definition of "raise the event" you have.

As I said before, of course if you define "raise the event" to be
"invoke the subscribers from within the class declaring the event", then
the answer to the qeustion becomes "yes", but in a completely trivial,
uninteresting way.

Pete
 
Ad

Advertisements

T

Tony Johansson

Peter Duniho said:
I do understand what you mean. But you don't appear to understand what I
mean.

Assuming this code:

Action action1 = () => Console.WriteLine("event");
Action action2 = action1;

…what significant difference is there between this:

action1();

…and this:

action2();

?

Answer: none. The name of the variable used to cause the delegate
instance to be invoked is irrelevant. What's important is that it was
invoked.

So, assuming this code:

class Test
{
public event Action TestEvent;

public Action TestEventAction { get { return TestEvent; } }

void RaiseTestEvent()
{
// Raising inside class:
TestEvent();
}
}

class Outside
{
void Method()
{
Test test = new Test();

test.TestEvent += () => Console.WriteLine("event");

// Raising outside class:
test.TestEventAction();
}
}

…there is also no difference between the code commented "// Raising inside
class" and the code commented "// Raising outside class".

Most classes aren't designed this way, and of the remaining few that are,
almost certainly they should not have been either. But the fact that it
_can_ happen means that the answer to your question is basically, no…to
raise the event does _not_ require the code doing that to be inside the
class where the event is declared. That's true no matter what non-trivial
definition of "raise the event" you have.

As I said before, of course if you define "raise the event" to be "invoke
the subscribers from within the class declaring the event", then the
answer to the qeustion becomes "yes", but in a completely trivial,
uninteresting way.

Pete

Good explanation Pete!
Yes I now understand this completeley. The reason I have asked is because of
this text in a book.
It says "Important. Events has a very useful built-in sequrity function. A
public event can only be raised by methods within the class that define the
event. All attempts to raise the event outside the class will result in
compile errror."
When I now know how this works I consider this text to be incorrect.

//Tony
 
Ad

Advertisements

P

Peter Duniho

Tony said:
[...]
It says "Important. Events has a very useful built-in sequrity function. A
public event can only be raised by methods within the class that define the
event. All attempts to raise the event outside the class will result in
compile errror."
When I now know how this works I consider this text to be incorrect.

I consider it incomplete, which I suppose could be the same as
incorrect. That is, if you follow the default implementation for
events, it's "correct enough". But yes, as written the text is not
precisely correct.

Pete
 

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

Similar Threads

struggling with events 4
Use of event handling 6
Handling Events in C#.NET 5
Raise Event in .NET 3,5 style property? 1
event and delegate 1
Delegating Events 1
about events and delegate 2
Events 3

Top