Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

OOP problem: multiple inheritance througt interfaces?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gambero - 21 Mar 2008 19:50 GMT
Hi, i have this classes:

class Db
class SqlDb : Db
class OracleDb : Db
class DbConverter
class SqlDbConverter : DbConverter
class OracleDbConverter : DbConverter

now,
in SqlDbConverter classe i would like to use DbConverter methods AND SqlDb
methods,
in OracleDbConverter class i would like to use DbConverter methods AND
OracleDb methods,

this is possible in c++ inherit a class from 2 base classes but with .net i
can iherit only from 1 class.

this becouse i would use common methods of DbConverter class for
SqlDbConverter objects and OracleDbConverter objects AND use common methods
of Db class for SqlDb objects and OracleDb objects.

now, c# implements multiple interface inheritance but i think it not solve
my problem...

any help? Thanks!
Peter Duniho - 21 Mar 2008 20:11 GMT
> [...]
> this becouse i would use common methods of DbConverter class for  
[quoted text clipped - 3 lines]
> now, c# implements multiple interface inheritance but i think it not  
> solve my problem...

If you want your OracleDbConverter class to itself expose the interface  
defined by OracleDb and DbConverter class, in a way that allows it to be  
treated as either class, that's not possible in C#.

However, using interfaces certainly is one possible work-around, depending  
on what you really need to accomplish.  If you can create an interface  
that describes either the Db or DbConverter class (or both), then your  
OracleDbConverter class can then implement that interface (or interfaces)  
using composition.  That is, inside the OracleDbConverter it would have an  
instance of the class implementing the interface, which is uses to  
delegate the handling of the interface to.

Then where you would have required the "Db" or "DbConverter" class,  
instead you can require the relevant interface.  Since your  
OracleDbConverter class would implement the interface, it would be usable  
there.

It's not quite as convenient as true multiple inheritance, but it does  
avoid some of the complexity that true MI can introduce.

Pete
Gambero - 21 Mar 2008 20:23 GMT
> [...]
> However, using interfaces certainly is one possible work-around, depending
[quoted text clipped - 9 lines]
> OracleDbConverter class would implement the interface, it would be usable
> there.

but with interfaces i have to IMPLEMENT ALL the code also for common
methods...
my goal is to derive from a common classes and implement only specialized
methods.
so that

SqlDb and OracleDb can use Db methods and SqlDbConverter and
OracleDbConverter can use Db and DbConverter methods.

there is a work-around for this?
Thanks
Peter Duniho - 21 Mar 2008 20:40 GMT
> but with interfaces i have to IMPLEMENT ALL the code also for common  
> methods...

Yes, you do.  I did say it's less convenient.  The VS IDE will  
auto-generate the interface stubs for you if you ask it to, but  
yes...you'll have to go through each one and add the appropriate calls  
within each stub.

> my goal is to derive from a common classes and implement only  
> specialized methods.
> so that
>
> SqlDb and OracleDb can use Db methods and SqlDbConverter and  
> OracleDbConverter can use Db and DbConverter methods.

I do understand how inheritance of concrete classes works, thank you.  
I've already explained that C# doesn't support multiple inheritance of  
classes, and I've already described the alternative.  As long as you can  
restructure your design so that interfaces can be used for your  
references, rather than actual classes, it will work, albeit with more  
typing effort on your part.

> there is a work-around for this?

See above.

Pete
Ignacio Machin ( .NET/ C# MVP ) - 21 Mar 2008 20:35 GMT
On Mar 21, 2:50 pm, "Gambero" <cgamberiniNON_VOGLIO_S...@libero.it>
wrote:
> Hi, i have this classes:
>
[quoted text clipped - 22 lines]
>
> any help? Thanks!

Can you use composition?
SqlDbConverter can instantiate an instance of SqlDb

Otherwise you have to rethink your structure
Gambero - 21 Mar 2008 21:02 GMT
>Can you use composition?
>SqlDbConverter can instantiate an instance of SqlDb

incapsulate a Db object in DbConverter class is a good alternative,
but in this way i have to change access modifiers of low-level methods
implemented in Db class to public for use in DbConverter methods,
and i would like that low-level methods remain private usable only within
the class or in derived classes..

thanks for suggestion!

any other idea?

Thanks
Ben Voigt [C++ MVP] - 21 Mar 2008 22:55 GMT
>> Can you use composition?
>> SqlDbConverter can instantiate an instance of SqlDb
[quoted text clipped - 4 lines]
> and i would like that low-level methods remain private usable only
> within the class or in derived classes..

That's what the "internal" access modifier is for...

> thanks for suggestion!
>
> any other idea?
>
> Thanks
Gambero - 22 Mar 2008 10:52 GMT
>> incapsulate a Db object in DbConverter class is a good alternative,
>> but in this way i have to change access modifiers of low-level methods
[quoted text clipped - 3 lines]
>
> That's what the "internal" access modifier is for...

good suggestion, but i have Db, SqlDb and OracleDb in one assembly, and
DbConverter, SqlDbConverter and OracleDbConverter in another assembly.
internal restrict the visibility only within the current assembly... is not
my solution..
Thanks

other suggestions?
Ben Voigt [C++ MVP] - 24 Mar 2008 14:57 GMT
>>> incapsulate a Db object in DbConverter class is a good alternative,
>>> but in this way i have to change access modifiers of low-level
[quoted text clipped - 12 lines]
>
> other suggestions?

InternalsVisibleToAttribute ?

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.