Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6227665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:15:43+00:00 2026-05-24T09:15:43+00:00

This is abit difficult to word, so I am going to rely mostly on

  • 0

This is abit difficult to word, so I am going to rely mostly on code.

BTW if you can word the question in a better light please dont hesitate giving your 2c!

class CustomEventArgs : EventArgs
{
    public delegate void CustomEventHandler( Object sender, CustomEventArgs args );

    public int data;

    public CustomEventArgs (int _data)
    {
        data = _data;
    }
}

This is the event that we will be using in this example.

class EventGenerator
{
    public event CustomEventArgs.CustomEventHandler WeOccasion;

    public EventGenerator ()
    {
        Task.Factory.StartNew( () =>
            {
                var index = 1;

                // just loop and generate events every now and then
                while (true)
                {   
                    Thread.Sleep( 1000 );
                    WeOccasion( this, new CustomEventArgs (++index));
                }
            });
    }
}

This class just loops through firing off CustomEventHandler events.

class EventActivity
{
    // EventActivity has an event of the same type as EventGenerator's 
    public event CustomEventArgs.CustomEventHandler WeOccasion;

    // this is the part I cant seem to get right
    public event CustomEventArgs.CustomEventHandler Source ( get; set; }

    public bool Active {
        set
        {
            if (value)
            {
                Source += DoWork;
            }
            else
            {
                Source -= DoWork;
            }
        }
    }

    private void DoWork( Object sender, CustomEventArgs frame);
}

Here is where I really need help. I want almost a pointer to an event in an another class of type CustomEventHandler that I can later assign event handlers to when I activate the activity.

Here is a usage example wrapped in a class;

class EventAssigner
{
    EventGenerator Generator;
    EventActivity DoSomeThing1;
    EventActivity DoSomeThing2;

    public EventAssigner ()
    {
        // init
        Generator = new EventGenerator();
        DoSomeThing1 = new EventActivity();
        DoSomeThing2 = new EventActivity();

        // assign sources
        DoSomeThing1.Source = Generator.WeOccasion;
        DoSomeThing2.Source = DoSomeThing1.WeOccasion;

        // activate the first activity
        DoSomeThing1.Active = true;
    }

    public void Activate2()
    {
        // activate the second activity
        DoSomeThing2.Active = true;
    }

    public void Deactivate2()
    {
        // deactivate the second activity
        DoSomeThing2.Active = false;
    }
}

Obiously this code doesnt work, and I suppose thats what I am asking. Can you get this design pattern to work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:15:44+00:00Added an answer on May 24, 2026 at 9:15 am

    What you’re asking to do isn’t really possible with .NET events, and probably isn’t as desirable as you might think. A bit of background should help explain why:

    Properties have a basic pattern with get and set operations. These are invoked by accessing the property (for a get) and an assignment to the property (for a set):

    var x = instance.Prop1; // access
    instance.Prop1 = x; // assignment
    

    When you access an event from outside the class (i.e. instance.Event) you are given the “public” face, which, like properties, has two operations: add handler and remove handler. These are invoked using the += and -= operators.

    instance.Event += this.Handler; // add
    instance.Event -= this.Handler; // remove
    

    The important thing to notice that it doesn’t have a “get” operation – there is no way to get a reference to the event outside the class; you can only modify the handlers registered.

    When you access an event from within a class, you are given the “private” face, which is essentially a special collection of delegates (function pointers) to the registered event handlers. When you invoke the delegate, you’re actually asking the framework to iterate through the registered event handlers and invoke those.

    if(this.Event != null)
    {
        this.Event.Invoke(e, args); // raise event
    }
    

    This separation of public face and private face is what allows you have a nice simple event keyword which magically gives you an event. It is also what stops you passing a reference to the event around.

    To pass the event into registration methods, you have to pass the object the event is attached to. If you have multiple classes which implement the same event and you want to register them all in the same way, you should have them implement an interface with the event (yes, events can be on interfaces) and write your method to accept the interface as an argument.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This might be a bit difficult to explain in writing, so please bear with
This is a bit of a difficult problem for me to word, and I
The title is a bit difficult to put together for this question. Say I
This is a bit difficult to explain, so please bear with me. I am
So this is going to be a bit difficult to explain, but I'll give
If this question is totally unsuitable, please forgive me. I don't know anything about
( This question about refactoring F# code got me one down vote, but also
I've been looking for the answer to this question but it seems quite difficult
I am having a bit of difficulty with this current code I have set
This is a bit of a long shot, but if anyone can figure it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.