About Event Handlers, Delegates and Scopes

Discussion in 'MCSD' started by Alex, May 17, 2004.

  1. Alex

    Alex Guest

    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.
    Alex, May 17, 2004
    #1
    1. Advertising

  2. >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
    The Poster Formerly Known as Kline Sphere, May 18, 2004
    #2
    1. Advertising

  3. 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" <> wrote in message
    news:e5zFYn#...
    > 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.
    >
    >
    Daniel Joskovski, May 19, 2004
    #3
  4. >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
    The Poster Formerly Known as Kline Sphere, May 19, 2004
    #4
  5. Alex

    Alex Guest

    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:...
    > >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
    Alex, May 20, 2004
    #5
  6. >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
    The Poster Formerly Known as Kline Sphere, May 20, 2004
    #6
  7. Alex

    Alex Guest

    I agree with you, with one exception....I am not missing the point ;-)

    "The Poster Formerly Known as Kline Sphere" <.> wrote in message
    news:...
    > >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
    Alex, May 21, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Au79
    Replies:
    0
    Views:
    359
  2. Ken

    Delegates

    Ken, May 4, 2005, in forum: MCAD
    Replies:
    1
    Views:
    360
    Mark Newby
    May 5, 2005
  3. Civet
    Replies:
    1
    Views:
    1,447
    Ghett Rheel
    Aug 17, 2009
  4. Civet
    Replies:
    0
    Views:
    526
    Civet
    Aug 17, 2009
  5. Civet
    Replies:
    0
    Views:
    346
    Civet
    Aug 17, 2009
Loading...

Share This Page