interfaces and overloaded functions

G

Guest

We are in the process of moving a number of classes to interfaces. Some of
the old code involves overload functions based on the derived type. Is there
a way to cast an interface to a derived class through knowlegde of the object
itself?

public class MyClass
{
//old code
//private Polymorphic1 polymorphic1 = null;
//private Polymorphic2 polymorphic2 = null;

//new code. Use as an interface
private IPolymorphic polymorphic1 = null;
private IPolymorphic polymorphic2 = null;

public MyClass()
{
this.InstatiateInterfaces();
}

private void InstatiateInterfaces()
{
this.polymorphic1 = new Polymorphic1();
this.polymorphic2 = new Polymorphic2();
}

public void PrintAll()
{
// This code no longer works.
//this.Print(this.polymorphic1);
//this.Print(this.polymorphic2);

//This is now needed
this.Print((Polymorphic1)this.polymorphic1);
this.Print((Polymorphic2)this.polymorphic2);

//is it possible to do something like....
//this.Print((this.polymorphic1.GetType())this.polymorphic1);
//this.Print((this.polymorphic2.GetType())this.polymorphic2);
}

private void Print(Polymorphic1 polyClass)
{
//do stuff...
Console.WriteLine(polyClass.GetValue());
}
private void Print(Polymorphic2 polyClass)
{
//do stuff...
Console.WriteLine(polyClass.GetValue());
}
}

interface IPolymorphic
{
string GetValue();
}

public class Polymorphic1 : IPolymorphic
{
public string GetValue()
{
return "Polymorphic1";
}

//specific class functionalty here....
}

public class Polymorphic2 : IPolymorphic
{
public string GetValue()
{
return "Polymorphic2";
}

//specific class functionalty here....
}
 
D

Daniel O'Connell [C# MVP]

Stedak said:
We are in the process of moving a number of classes to interfaces. Some of
the old code involves overload functions based on the derived type. Is
there
a way to cast an interface to a derived class through knowlegde of the
object
itself?

public class MyClass
{
//old code
//private Polymorphic1 polymorphic1 = null;
//private Polymorphic2 polymorphic2 = null;

//new code. Use as an interface
private IPolymorphic polymorphic1 = null;
private IPolymorphic polymorphic2 = null;

public MyClass()
{
this.InstatiateInterfaces();
}

private void InstatiateInterfaces()
{
this.polymorphic1 = new Polymorphic1();
this.polymorphic2 = new Polymorphic2();
}

public void PrintAll()
{
// This code no longer works.
//this.Print(this.polymorphic1);
//this.Print(this.polymorphic2);

//This is now needed
this.Print((Polymorphic1)this.polymorphic1);
this.Print((Polymorphic2)this.polymorphic2);

//is it possible to do something like....
//this.Print((this.polymorphic1.GetType())this.polymorphic1);
//this.Print((this.polymorphic2.GetType())this.polymorphic2);
}

Not in any syntactical way. You would have to do your own dispatching. You
could create a table of type and delegate pairs or simply use a bunch of
if\else if statements to chose which overload you want.

Now, more importantly, you should probably revisit the decision to convert
to interfaces. If functionality cannot be encapsulated in an interface then
the class has no business *being* in an interface.
Your example here requires no particular special code, the proper GetValue
method should be called in any case. I assume the real example is more
complex?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top