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

The Archive Base Latest Questions

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

Both of these classes contain another private class that raises events. These two classes

  • 0

Both of these classes contain another private class that raises events. These two classes then re-raise these events to clients.

Unfortunately, each of the two classes has this exact same code:

public class FirstClass
{
    public delegate void FooEventHandler(string foo);
    public delegate void BarEventHandler(string bar);
    public delegate void BazEventHandler(string baz);

    public event FooEventHandler Foo;
    public event BarEventHandler Bar;
    public event BazEventHandler Baz;

    private PrivateObject privateObject;

    public FirstClass()
    {
        privateObject.Foo += FirstClass_Foo;
        privateObject.Bar += FirstClass_Bar;
        privateObject.Baz += FirstClass_Baz;
    }

    private void FirstClass_Foo(string foo)
    {
        if (Foo != null)
        {
            Foo(foo);
        }
    }

    private void FirstClass_Bar(string bar)
    {
        if (Bar != null)
        {
            Bar(bar);
        }
    }

    private void FirstClass_Baz(string baz)
    {
        if (Baz != null)
        {
            Baz(baz);
        }
    }
}

As you can see, I have to re-raise events from a private object. It is redundant. I tried using inheritance and placing this duplicate code in a base class but I keep getting errors like:

The event ‘BaseClass.Foo’ can only appear on the left hand side of += or -= (except when used from within the type)

Does anyone know how to get rid of this duplicate code?

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

    what about exposing the events of the private object as properties of your wrapper? As in,

    public class ExternalClass
    {
        private InternalClass _internalObject = new InternalClass();
    
        public event InternalClass.someDelegate SomeEvent
        {
            add
            {
                _internalObject.SomeEvent += value;
            }
            remove
            {
                _internalObject.SomeEvent -= value;
            }
        }
    }
    
    public class InternalClass
    {
        public delegate void someDelegate(string input);
        public event someDelegate SomeEvent;
    }
    

    If you are familiar with c# Properties you probably already know the get and set keywords. The add/remove keywords are basically the same thing, only they are fired when you attempt to add or remove a value to your property.

    So, when you command to (un)register your delegate to ExternalClass.SomeEvent, you are actually (un)registering to the InternalClass.SomeEvent event.

    If you are not familiar with c# Properties, http://msdn.microsoft.com/en-us/library/x9fsa0sw(v=vs.80).aspx would help you.

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

Sidebar

Related Questions

I have two classes, call them A and B. They both contain a Loader
What are the most common problems that can be solved with both these data
I'm trying to write a python regular expression that will match both these URLs:
I'm trying to export classes from a DLL that contain objects such as std::vectors
While editing an aspx file I found both these opening tags used for seemingly
I understand it's a standard practice to look at both these variables. Of course
Both of these appservers are at least in part OSGI based. One (Glassfish) is
Are both of these versions OK or is one of them to prefer? #!/usr/bin/env
I have heard both of these terms used, are they the same thing or
I've found myself evaluating both of these libs. Apart from what the GraphicsMagick comparison

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.