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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:46:08+00:00 2026-05-25T21:46:08+00:00

Ok so let’s say I have two classes, class A and class B. They

  • 0

Ok so let’s say I have two classes, class A and class B. They both need to perform a common operation but it wouldn’t make sense to have them both derive from the same object so instead we go with composition. Basically, “has a” makes sense where “is a” doesn’t.

Now each of the two classes instantiate this object and use it to perform the common operation and all is good. Everything is fine, now.

The problem arises when each of these two classes have to expose an event that the common routine must raise. Uh oh. Now what?

Well, we could do something like this in each of the two classes:

public event EventHandler CommonEvent
{
    add { theCommonObject.CommonEvent += value; }
    remove { theCommonObject.CommonEvent -= value; }
}

But now this code needs to be duplicated in each of the two classes and readers of the code may be somewhat confused by this code as well. add and remove are not very commonly seen keywords, at least.

Still, there is duplicate code in both of the two classes and not only that, but this solution will not always work!

For example, what if theCommonObject is locally instantiated in a single method and its scope is only within that one method.

Well then, in that case we cannot even use the shortcut code posted earlier and we have to completely duplicate the code in both classes. Not only that but it just screams inelegance because we’re basically just re-raising a copy of the event to clients.

Example code that would have to be duplicated in each class in this case:

public event EventHandler SomeCommonEvent;

private void SomeMethod()
{
    theCommonObject = new theCommonObject();

    theCommonObject.DoSomething();

    theCommonObject.SomeCommonEvent += thisClass_SomeCommonEvent;
}

private void thisClass_SomeCommonEvent(object sender, EventArgs e)
{
    var handler = SomeCommonEvent;

    if (handler != null)
    {
        handler(this, e);
    }
}

As you can see, in both cases there is duplicate code but in the second there is even more because the object is local to a method rather than a member variable.

I cannot really think of a way around this, other than using inheritance but I don’t think it makes semantic sense to do so, and many often claim that composition is superior to inheritance in terms of complexity-reduction, ease-of-understanding, et cetera.

  • 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-25T21:46:08+00:00Added an answer on May 25, 2026 at 9:46 pm

    You say you have a “has-a” relationship. From that, and based on your proposed solution that duplicates code, I assume your class A looks something like:

    public class A
    {
        private SomeCommonObject theCommonObject;
        public event EventHandler CommonEvent
        {
            add { theCommonObject.CommonEvent += value; }
            remove { theCommonObject.CommonEvent -= value; }
        }
    
        public A()
        {
            theCommonObject = new SomeCommonObject();
        }
    }
    

    Where’s the problem? theCommonObject can never be “locally instantiated in a single method and its scope is only within that one method.”

    Yes, add and remove are not commonly used. So what? Clients of the class never see the add and remove. They still subscribe to the event using the traditional += syntax. The add and remove syntax is a non-issue as far as clients are concerned. And you, the developer, who is creating the class, are expected to know about these things.

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

Sidebar

Related Questions

Let's say that I have classes like this: public class Parent { public int
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have two tables orgs and states orgs is (o_ID, state_abbr) and
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have the following entity: public class Store { public List<Product> Products
Let say I have two UIViews: View1: - bounds: 0, 0, 20, 20 -
Let's say I have two assemblies: BusinessLogic and Web. BusinessLogic has an application setting
Let's say I have a simple UserControl with no codebehind: <UserControl xmlns= .... x:Class=TrafficLight.LightControl>
Let's say I have the following models class Photo(models.Model): tags = models.ManyToManyField(Tag) class Tag(models.Model):
Let's say I'm building a data access layer for an application. Typically I have

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.