2

I had an API with methods and throughout the program I accessed the members of that API simply like:

Class API
{
  public method1{}
  public method3{}
}
API _api = new API(); // Inside constructor
_api.method1();   // I have accessed the api like this in so many places within my GUI

Now I got a new API with different methods and some overlaping:

Class new_API
{
  public method2{}
  public method3{}
}

My Question is how can I still keep a single api instantiation but instantiate different api classes?

   API _api = new new_API() or new API();

FYI, this is the new path I'm taking after trying to just have one big class and hide the non common methods by passing extra parameter to the class instatiation: How to hide functions within classes in c#?

Thanks in Advance.

2
  • 1
    From reading the previous question you've linked, it almost sounds like you don't really want any inheritance relationship at all. You talk about code paths being able to determine which object to create and which methods to call. That seems to dictate they should be different, concrete types. Commented Jan 26, 2012 at 19:43
  • THat is exactly right. The main issue here is but only one single instantiation with two different APIS with bit similarity. Commented Jan 26, 2012 at 20:13

5 Answers 5

2

If both API classes have a shared inheritance chain, you can use the base type (base class, abstract class or interface) as the variable type.

You will need to inject the actual type or use a factory to retrieve it.

If the API classes don't have a shared ancestor, create an interface that both can implement.

Sign up to request clarification or add additional context in comments.

Comments

1

You should create an interface containing the methods and implement it in both classes.

Comments

0

In addition to what everyone said, there's dynamic keyword that lets you call objects in explicitly type-unsafe manner. But that's playing with fire. I won't do it.

Comments

0
  • The bizzar way, is:

    dynamic myapiobject = new new_API() or new API();

    Available only from .NET Framework 4.0.

  • Derive new one from old one, if you can

    public class new_API : API

hope this helps.

Comments

0

Option 1: Interface

Class API : IAPI
{ 
  public method1{} 
  public method3{} 
} 

Class new_API : IAPI
{ 
  public method2{} 
  public method3{} 
} 

interface IAPI{
    void method3{} //just the shared items
}

Method 2: Late Binding

dynamic _api = new new_API() or new API(); 

Method 3: Inheritance

Class API : APIBase
{ 
  public method1{} 
  public override method3{} 
} 

Class new_API : APIBase
{ 
  public method2{} 
  public override method3{} 
} 

abstract class APIBase{
    abstract method3{} 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.