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 7735267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:27:30+00:00 2026-06-01T07:27:30+00:00

I have run into a bit of a design issue with my code. I

  • 0

I have run into a bit of a design issue with my code.
I have a object that creates a child object (the child could then create another child, etc), and both objects subscribe to the same event.
But, I only want the most child object to receive the event.

Overview of what my project is:
I am creating a IVR system. When a user calls into the system, the user will have X menu choices. Based on what the user chooses they will have a sub menu of choices, and so on and so on. I am using State Machines for this. Every State Machine needs to “listen” for when the user presses a number on their phone. But only the current State Machine needs to process the entered number. Each State Machine can create a new State Machine to represent the sub menu.

Here is some sample code:

Base class:

public delegate void DoSomething(object sender, EventArgs data);
public class Base
{
    public event DoSomething myEvent;
    private IObject foo;

    public Base ()
    {
        foo = new myObjectA(this);
    }

    public void SomeAction()
    {
        ((myObjectA)foo).CreateChild();
    }

    public void EventFired()
    {
        if (myEvent != null)
        {
            myEvent(this, new EventArgs());
        }
    }
}

ObjectA:

class myObjectA : IObject
{
    private Base theCallingObject;
    private IObject child;
    public myObjectA (Base _base)
    {
        theCallingObject = _base;
        theCallingObject.myEvent += new DoSomething(theCallingObject_myEvent);
    }

    public void CreateChild()
    {
        child = new myObjectB(theCallingObject);
    }

    void theCallingObject_myEvent(object sender, EventArgs data)
    {
        // Handle event
        MessageBox.Show("myObjectA");
    }
}

ObjectB:

class myObjectB : IObject
{
    private Base theCallingObject;
    public myObjectB (Base _base)
    {
        theCallingObject = _base;
        theCallingObject.myEvent += new DoSomething(theCallingObject_myEvent);
    }

    void theCallingObject_myEvent(object sender, EventArgs data)
    {
        // Handle event
        MessageBox.Show("myObjectB");
    }
}

Now when I do this:

Base blah = new Base();
blah.SomeAction();
blah.EventFired();

I get message boxes for both A and B.
I need to implement Base so that only myObjectB gets the event.
I will have hundreds of myObject’s so I need a implementation at the Base level and NOT the myObject level. Plus, handling it at the myObject level would still require the event to be fired causing performance issues if there are hundreds of objects.

One solution I have considered is when myObjectA creates the child, unsubscribe from the event, then resubscribe when we get back to the myObjectA level. However I feel something better could be done.

Anyone have any ideas?

Edit: Using payo’s input I have come up with this:

public delegate void DoSomething(object sender, EventArgs data);
public class Base
{
    private IObject foo;
    private List<DoSomething> _myEventStorage;

    public event DoSomething myEvent
    {
        add
        {
            _myEventStorage.Insert(0, value);
        }
        remove
        {
            _myEventStorage.Remove(value);
        }
    }

    public Base ()
    {
        _myEventStorage = new List<DoSomething>();
        foo = new myObjectA(this);
    }

    public void SomeAction()
    {
        ((myObjectA)foo).CreateChild();
    }

    public void EventFired()
    {
        _myEventStorage[0].Invoke(this, new EventArgs());
    }
}
  • 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-06-01T07:27:31+00:00Added an answer on June 1, 2026 at 7:27 am

    For events, each subscriber is queued up (put at end of list), a FIFO model. You want the most-child object to ‘own’ the event, not just subscribe and be part of some abstract list of other unknown objects.

    I would provide a new model that represents what you are trying to do. This might be what Jason recommended: (he posted his answer as I was typing this out)

    public class Base
    {
      private DoSomething _myEventStorage;
      public event DoSomething myEvent
      {
        add
        {
          _myEventStorage = value;
        }
        remove
        {
          _myEventStorage -= value;
        }
      }
    ...
      public void EventFired()
      {
        if (_myEventStorage != null)
        {
          _myEventStorage(this, new ChainEventArgs());
        }
      }
    }
    

    This calls last ONLY. Another option (to add to this custom add/remove) would be to provide a derived EventArgs:

    public class ChainEventArgs : EventArgs
    {
      public bool Handled { get; set; }
    }
    public delegate void DoSomething(object sender, ChainEventArgs data);
    
    ...
    
      public event DoSomething myEvent
      {
        add
        {
          var temp = _myEventStorage;
          _myEventStorage = null;
          _myEventStorage += value;
          _myEventStorage += temp; // now all are called, but FILO
        }
        remove
        {
          _myEventStorage -= value;
        }
      }
    

    At this point, you can either check Handled on each IObject

    void theCallingObject_myEvent(object sender, ChainEventArgs data)
    {
      if (data.Handled)
        return;
    
      if (I_want_to_block_parents)
        data.Handled = true;
      // else leave it false
    }
    

    Or, add some complexity to your Base class and stop calling up the chain (let’s the children have no need to check Handled). I’ll show the solution with a List<> of delegates, but some MulticaseDelegate casts and calls could do the same. I just feel the List<> code might be more readable/maintainable.

    public class Base
    {
      private List<DoSomething> _myEventStorage;
      public event DoSomething myEvent
      {
        add
        {
          _myEventStorage.Insert(0, value);
        }
        remove
        {
          _myEventStorage.Remove(value);
        }
      }
    ...
      public void EventFired()
      {
        var args = new ChainEventArgs();
        foreach (var handler in _myEventStorage)
        {
          handler(this, args);
          if (args.Handled)
            break;
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have run into a bit of a tricky problem in some C++ code,
I have run into an issue with WPF and Commands that are bound to
I have run into a bit of a snag in my code. I am
Hey.. I have run into a bit of a problem with my python code..
I've run into a bit of an issue designing RegEx in C#. I have
I have run into a bit of a problem here: I had a problem-specific
I seem to have run into a bit of trouble this morning with removeClass
I've run into an issue that I can't seem to figure out the solution
I have run into a rather weird little problem. In the following code I
I am a bit new at C# and I have run into a string

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.