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

  • Home
  • SEARCH
  • 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 99087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:19:14+00:00 2026-05-11T00:19:14+00:00

What is the best design decision for a ‘top-level’ class to attach to an

  • 0

What is the best design decision for a ‘top-level’ class to attach to an event to a class that may be ‘5+ layers down in the callstack?

For example, perhaps the MainForm has spawned an object, and that object has spawned a callstack of several other object calls. The most obvious way would be to chain the event up the object hierarchy, but this seems messy and requires a lot of work.

One other solution ive seen is to use the observer pattern by creating a publically accessible static object which exposes the event, and acts as a proxy between the bottom-level object, and the top-level ‘form’.

Any recommendations?

Here’s a pseudo-code example. In this example, the MainForm instantiates ‘SomeObject’, and attaches to an event. ‘SomeObject’ attaches to an object it instantiates, in an effort to carry the event up to the MainForm listener.

class Mainform {    public void OnLoad()    {       SomeObject someObject = new SomeObject();       someObject.OnSomeEvent += MyHandler;       someObject.DoStuff();    }     public void MyHandler()    {    } }    class SomeObject {    public void DoStuff()    {       SomeOtherObject otherObject = new SomeOtherObject();       otherObject.OnSomeEvent += MyHandler;       otherObject.DoStuff();    }      public void MyHandler()    {       if( OnSomeEvent != null )           OnSomeEvent();    }      public event Action OnSomeEvent; } 
  • 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. 2026-05-11T00:19:14+00:00Added an answer on May 11, 2026 at 12:19 am

    If your application isn’t based on Composite UI Application Blocks, the easiest solution is to put a ‘listener’ class between Main form and your other components which both classes can easily access. Conceptually, the classes are laid out as follows:

          ----------         ----------------     | MainForm |       | Some Component |       ---------         ----------------           |                    |       Hooks onto            Notifies           |                    |            \                  /             -----------------            | Proxy Notifier  |             ----------------- 

    Here’s some example code:

    using System; using System.Collections.Generic; using System.Linq; using System.Text;  namespace ConsoleApplication1 {     class Program     {         static void Main(string[] args)         {             FakeMainForm form = new FakeMainForm();             form.CreateComponentAndListenForMessage();             Console.ReadKey(true);         }     }      class FakeMainForm     {         public FakeMainForm()         {             Listener.AddListener(MessageRecieved);         }          void MessageRecieved(string msg)         {             Console.WriteLine('FakeMainForm.MessageRecieved: {0}', msg);         }          public void CreateComponentAndListenForMessage()         {             ComponentClass component = new ComponentClass();             component.PretendToProcessData();         }     }      class Listener     {         private static event Action<string> Notify;          public static void AddListener(Action<string> handler)         {             Notify += handler;         }          public static void InvokeListener(string msg)         {             if (Notify != null) { Notify(msg); }         }     }      class ComponentClass     {         public void PretendToProcessData()         {             Listener.InvokeListener('ComponentClass.PretendToProcessData() was called');         }     } } 

    This program outputs the following:

    FakeMainForm.MessageRecieved: ComponentClass.PretendToProcessData() was called

    This code allows you to invoke methods directly on any listener, no matter how far apart they are in the call stack.

    Its easy to rewrite your Listener class so that its a little more generic and works on different types, but you should get the idea.

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

Sidebar

Ask A Question

Stats

  • Questions 73k
  • Answers 73k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer SELECT OBJECT_NAME(parent_id) AS [Table], OBJECT_NAME(object_id) AS TriggerName FROM sys.triggers WHERE… May 11, 2026 at 1:57 pm
  • added an answer You can't use the this keyword when chaining constructors essentially… May 11, 2026 at 1:57 pm
  • added an answer I would question if there is not some UML2Tools already… May 11, 2026 at 1:57 pm

Related Questions

What is the best design decision for a 'top-level' class to attach to an
What is the best way to design an interface so that very long drop
In short: what is the best way to design and implement a factory+plugin mechanism,
What is the best solution for maintaining backup and revision control on live websites?
What is the best way to verify/test that a text string is serialized to
What is the best way to make a Delphi application (Delphi 2007 for Win32)
Ever since I started using .NET, I've just been creating Helper classes or Partial
Image a Button on your windows form that does something when being clicked. The
I have a project that I have been working on for a while, just

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.