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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:46:33+00:00 2026-05-17T20:46:33+00:00

Let’s say you have two different C# classes A and B that while not

  • 0

Let’s say you have two different C# classes A and B that while not deriving from the same base class do share some of the same names for methods. For example, both classes have a connect and a disconnect method, as well as several others. I want to be able to write code once that will work with both types.

Here is a simplified example of what I would like to do:

public void make_connection(Object x)
{
  x.connect() ;
  // Do some more stuff...
  x.disconnect() ;
  return ;
}

Of course, this does not compile as the Object class does not have a connect or disconnect method.

Is there a way to do this?

UPDATE. I should have made this clear from the start: I only have the DLLs for A and B and not the source.

  • 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-17T20:46:34+00:00Added an answer on May 17, 2026 at 8:46 pm

    You can use an interface to accomplish what you want to do.

    interface IConnectable
    {
        void Connect();
    
        void Disconnect();
    }
    

    Both A and B should implement IConnectable. Then use IConnectable instead of Object as the parameter type for your method and you should be all set.

    public void MakeConnection(IConnectable connectable)
    {
        connectable.Connect();
    
        // Do some more stuff...
    
        connectable.Disconnect();
    }
    

    Edit: Since you don’t have the source code, you have a couple of options:

    1. Use Max’s solution of using the dynamic keyword, (if you are using .NET 4.0)
    2. Use Steve’s solution of using casting and if/else statements
    3. Create wrapper classes for A and B and have them implement the interface (or use common abstract base class for them)

    For example:

    class AWrapper : IConnectable
    {
        private A obj;
    
        public AWrapper(A obj)
        {
            this.obj = obj;
        }
    
        public void Connect()
        {
            this.obj.Connect();
        }
    
        public void Disconnect()
        {
            this.obj.Disconnect();
        }
    
        // other methods as necessary
    }
    

    (BWrapper would be similar, just using B instead of A)

    Then you could create the wrappers and pass them into MakeConnection. It’s up to you how you want to do it. Depending on your situation, one method may be easier than the others.

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

Sidebar

Related Questions

Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say I'm building a data access layer for an application. Typically I have
Let me try to explain what I need. I have a server that is
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I have a drive such as C:\ , and I want to
Let's say you create a wizard in an HTML form. One button goes back,
I have a French site that I want to parse, but am running into
Let's aggregate a list of free quality web site design templates. There are a

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.