Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Computer Certification > MCSD > About Event Handlers, Delegates and Scopes

Reply
Thread Tools

About Event Handlers, Delegates and Scopes

 
 
Alex
Guest
Posts: n/a
 
      05-17-2004
1. Can an event handler access a private method or member of a class?
2. If so.. is that a language conceptual error?

Best regards, Alejandro Penate-Diaz.


 
Reply With Quote
 
 
 
 
The Poster Formerly Known as Kline Sphere
Guest
Posts: n/a
 
      05-18-2004
>1. Can an event handler access a private method or member of a class?

Event handler delegates are classes in themselves, which simply allow
for a type safe, object oriented, way of implementing callbacks.

The event handler delegate cannot directly access private or protected
data of the class which created it (although it could through
reflection if a reference to the creating object was somehow saved).

Naturally, the actual 'callback' method supplied by the creator can
access it's data, as this is not part of the event handler delegate
class. In addition, the 'callback' method cannot access data defined
in the event handler delegate class.

>2. If so.. is that a language conceptual error?


Certainly not.

Kline Sphere (Chalk) MCNGP #3
 
Reply With Quote
 
 
 
 
Daniel Joskovski
Guest
Posts: n/a
 
      05-18-2004
Yes he can.
Event system in .NET is composed from Event sender and Event receiver. Event
sender and Event receiver are tied together with instance of class
EventHandler. You declare and define instance of EventHandler for every
event you want to handle on the class level;

myObject.someEvent += new Eventhandler(myEventHandlingMetod);

in above line, it is doesn't matter what is the access modifier of my
EventHandlingMetod, it can be private, protected,public or whatever.

That is how I understand your question, sorry if I misunderstand something

Regards,
Daniel
"Alex" <(E-Mail Removed)> wrote in message
news:e5zFYn#(E-Mail Removed)...
> 1. Can an event handler access a private method or member of a class?
> 2. If so.. is that a language conceptual error?
>
> Best regards, Alejandro Penate-Diaz.
>
>



 
Reply With Quote
 
The Poster Formerly Known as Kline Sphere
Guest
Posts: n/a
 
      05-19-2004
>That is how I understand your question, sorry if I misunderstand something

Although the question was rather ambiguous, I believe the poster was
referring to EventHandler/Delegate classes, rather that the actual
listener which creates such classes, which does not have access to the
listener's private state.

Kline Sphere (Chalk) MCNGP #3
 
Reply With Quote
 
Alex
Guest
Posts: n/a
 
      05-20-2004
Hi. My question was based on the fact that the use of delegates is a
different way of using class members, and I was wondering how the scope
concept is handled in these cases. The answer of Daniel Joskovski is one way
of the analysis that I did too, and he is right. The other way around is not
possible to access private members of the class that holds the event class,
but some one suggested that there is a way and that is passing a reference
to the private member through the event parameters. But I still think that
the use of events is not a native concept of OOP and I was just curious
about that integration.

The use of events helps a lot, so my question is only for curiosity.



Alejandro.



"The Poster Formerly Known as Kline Sphere" <.> wrote in message
news:(E-Mail Removed)...
> >That is how I understand your question, sorry if I misunderstand

something
>
> Although the question was rather ambiguous, I believe the poster was
> referring to EventHandler/Delegate classes, rather that the actual
> listener which creates such classes, which does not have access to the
> listener's private state.
>
> Kline Sphere (Chalk) MCNGP #3



 
Reply With Quote
 
The Poster Formerly Known as Kline Sphere
Guest
Posts: n/a
 
      05-20-2004
>some one suggested that there is a way and that is passing a reference
>to the private member through the event parameters.


You can use reflection to get back to creating class. With refection
you can access private data of the parent - this is mad, and breaks
the concept of oop.

However, you seem to be missing the point. The actual 'method'
assigned to a delegate does not belong to the delegate, but to class
instance that created the delegate instance. The delegate class cannot
[directly] access data in the creating class, nor can the delegated
method access [directly] private data in the delegate class instance
itself. This is how it should be, and represents a much more cleaner
approach than using raw pointers (even with typedefs), as is the case
with C++, to implement a publisher\subscriber paradigm.

In a nutshell, all that is happening is that the delegate is calling a
method of one of it's members, i.e. the method.

>But I still think that
>the use of events is not a native concept of OOP and I was just curious
>about that integration.


It is a very well thought out approach

>The use of events helps a lot, so my question is only for curiosity.


They sure do, maybe have a go (using win32 sdk) at writing a win32
window's app or COM component in C (or even C++) and see how messy
things become!

Kline Sphere (Chalk) MCNGP #3
 
Reply With Quote
 
Alex
Guest
Posts: n/a
 
      05-21-2004
I agree with you, with one exception....I am not missing the point

"The Poster Formerly Known as Kline Sphere" <.> wrote in message
news:(E-Mail Removed)...
> >some one suggested that there is a way and that is passing a reference
> >to the private member through the event parameters.

>
> You can use reflection to get back to creating class. With refection
> you can access private data of the parent - this is mad, and breaks
> the concept of oop.
>
> However, you seem to be missing the point. The actual 'method'
> assigned to a delegate does not belong to the delegate, but to class
> instance that created the delegate instance. The delegate class cannot
> [directly] access data in the creating class, nor can the delegated
> method access [directly] private data in the delegate class instance
> itself. This is how it should be, and represents a much more cleaner
> approach than using raw pointers (even with typedefs), as is the case
> with C++, to implement a publisher\subscriber paradigm.
>
> In a nutshell, all that is happening is that the delegate is calling a
> method of one of it's members, i.e. the method.
>
> >But I still think that
> >the use of events is not a native concept of OOP and I was just curious
> >about that integration.

>
> It is a very well thought out approach
>
> >The use of events helps a lot, so my question is only for curiosity.

>
> They sure do, maybe have a go (using win32 sdk) at writing a win32
> window's app or COM component in C (or even C++) and see how messy
> things become!
>
> Kline Sphere (Chalk) MCNGP #3



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Digiscoping Acuter and Celestron Spotting Scopes and Zenith BiolamMicroscopes with Canon Powershot A590 IS Civet Digital Photography 0 08-17-2009 08:51 PM
Digiscoping Acuter and Celestron Spotting Scopes and Zenith BiolamMicroscopes with Canon Powershot A590 IS Civet Digital Photography 1 08-17-2009 03:25 PM
Event not being raised (how to get list of delegates) Karch ASP .Net 2 03-02-2007 07:50 PM
DropDownList event delegates studio60podcast@gmail.com ASP .Net 2 02-08-2007 07:02 PM
Event Delegates and Private scope Kevin Spencer ASP .Net 1 08-18-2003 05:05 PM



Advertisments