Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

polymorphism without inheritance

Posted on 2006-11-07
4
Medium Priority
?
553 Views
Last Modified: 2008-02-01
Hi

I have two classes which have exactly the same properties and methods. These classes aren't created by me. If they were I could have them inherit from the same class. Sadly i can't do that.

For example, lets say the class are of type ClassA and ClassB

Without inheritance, is there any way i can achieve polymorphisn. I woudl like to create a property to which I can assign either an object of ClassA and ClassB and then call the methods on this object which both of them have.

thanks a lot
andrea
0
Comment
Question by:andieje
  • 2
4 Comments
 

Author Comment

by:andieje
ID: 17888902
I've been thinking.

I do have some access to the code. I could use partial classes and give them  both a shared interface?
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 2000 total points
ID: 17888945
Exactly, you answered your own question. Interface is the way.
Write interface which contains all common properties and methods, derive both classes from this interface. Client code must call interface methods using interface reference.
0
 
LVL 13

Expert Comment

by:newyuppie
ID: 17889035
i dont know if i fully understand...

1) you have 2 classes which expose the same methods and properties, but obviously they perform different operations on these same methods otherwise whats the point of having 2 different same classes, right?

2)now you have created a property (on a third class, created by you) that instantiaties an object of type 1 or 2.

3) now when you say<<then call the methods on this object which both of them have.>>, i dont understand why you cant call the methods now as is. i mean, if proposition 1) and 2) are true, it would be very clear that if you instantiate the object of type 1 you will get different results than if you instantiate it as type 2 (type 1 i mean of class 1).

4) if 3 is true then what's the problem?

sorry if i dont understand the question maybe im very on the wrong track...?
NY
0
 

Author Comment

by:andieje
ID: 17889074
yippe, don't you just love partial classes
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Integration Management Part 2
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question