473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

interfaces and overloaded functions

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.polym orphic1);
//this.Print((this.polymorphic2.GetType())this.polym orphic2);
}

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....
}

Nov 16 '05 #1
1 1339

"Stedak" <St****@discussions.microsoft.com> wrote in message
news:78**********************************@microsof t.com...
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.polym orphic1);
//this.Print((this.polymorphic2.GetType())this.polym orphic2);
}


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?

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. ...
44
by: bahadir.balban | last post by:
Hi, What's the best way to implement an overloaded function in C? For instance if you want to have generic print function for various structures, my implementation would be with a case...
3
by: cybertof | last post by:
Hello, Is it possible in C# to have 2 overloaded functions with - same names - same parameters - different return type values If no, is it possible in another language ?
17
by: Picho | last post by:
Hi all, I popped up this question a while ago, and I thought it was worth checking again now... (maybe something has changed or something will change). I read this book about component...
9
by: Terry | last post by:
I am converting (attempting) some vb6 code that makes vast use of interfaces. One of the major uses is to be able to split out Read-only access to an obect. Let me give you a simple (contrived)...
18
by: _dee | last post by:
Question about best use of interfaces: Say there's a 'Master' class that needs to implement a few interfaces: class Master : I1, I2, I3 { } The actual code already exists in smaller...
2
by: desktop | last post by:
If a function should work with different types you normally overload it: void myfun(int a){ // do int stuff } void myfun(std::string str){ // do string stuff }
4
by: king kikapu | last post by:
Hi, i am trying, to no avail yet, to take a C#'s overloaded functions skeleton and rewrite it in Python by using closures. I read somewhere on the net (http://dirtsimple.org/2004/12/python-is-...
26
by: Chris Becke | last post by:
Given an interface (in the c++ sense, nothing more than a struct containing pure virtual methods) struct Iv1 { virtual method0()=0; }; And a class that implements the interface class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.