Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# code to handle different classes with same method names

Tags:

c#

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.

like image 384
rlandster Avatar asked Oct 29 '10 05:10

rlandster


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


3 Answers

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.

like image 55
Zach Johnson Avatar answered Oct 21 '22 04:10

Zach Johnson


This will work in C# 4:

public void make_connection(dynamic x)
{
  x.connect() ;
  // Do some more stuff...
  x.disconnect() ;
  return ;
}
like image 21
Max Avatar answered Oct 21 '22 04:10

Max


Try using an Interface rather.

Have a look at interface (C# Reference) and Interfaces (C# Programming Guide)

So something like

public interface IConnections
{
    void connect();
    void disconnect();
}

public class A : IConnections
{
    public void connect()
    {
        //do something
    }

    public void disconnect()
    {
        //do something
    }
}

public class B : IConnections
{
    public void connect()
    {
        //do something
    }

    public void disconnect()
    {
        //do something
    }
}

public void make_connection(IConnections x)
{
    x.connect();
    // Do some more stuff... 
    x.disconnect();
    return;
}
like image 21
Adriaan Stander Avatar answered Oct 21 '22 04:10

Adriaan Stander