471,319 Members | 3,299 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

How to inherit from two places?

I know inheriting from two different classes isn't allowed but here is
my situation. I'm creating a custom datagrid class that inherits from
DataGrid. I have another class (dgExt) with useful methods that will
be used in the customDataGrid() class. I can create a new dgExt
object in customDataGrid() and reference methods in dgExt. I'd
basically wrap any method I needed in dgExt and make the wrapper public
in customDataGrid. There's a scaling problem though.

If dgExt needs to change any of its method signatures, my code in
customDataGrid breaks. Inheritance probably doesn't help very much
here. However, by using wrappers, I'm having to completely copy the
parameters in the signatures of dgExt. Not exactly code reuse.

Is there a better way to do this?

Thanks,
Brett

Jan 20 '06 #1
9 4680
Create an interface that defines the functionality of dgExt, and have
both dgExt and your custom DataGrid implement the interface.

In the absence of multiple inheritance, it's the best you can do.

Jan 20 '06 #2
In CustomDataGrid, would that look like:

public class CustomDataGrid: inheritedClass, interfaceClass

Brett

Jan 20 '06 #3
Hello Brett,

The best way to go for this would be to go for the wrapper which would encapsulate
whats variable. Also, embibing the same priniciple, it might be much better
if you have data objects encapsulating the parameters passed to the dgExt
methods.

for ex. if your method now is

Foo(int a, int b, string c)

you might be better placed in a variable scene with

Foo ( FooData _fooData)

where

class FooData
{
int a;
int b;
int c;

//properties
}
HTH,
r.
I know inheriting from two different classes isn't allowed but here is
my situation. I'm creating a custom datagrid class that inherits from
DataGrid. I have another class (dgExt) with useful methods that will
be used in the customDataGrid() class. I can create a new dgExt
object in customDataGrid() and reference methods in dgExt. I'd
basically wrap any method I needed in dgExt and make the wrapper
public in customDataGrid. There's a scaling problem though.

If dgExt needs to change any of its method signatures, my code in
customDataGrid breaks. Inheritance probably doesn't help very much
here. However, by using wrappers, I'm having to completely copy the
parameters in the signatures of dgExt. Not exactly code reuse.

Is there a better way to do this?

Thanks,
Brett

Jan 20 '06 #4
Ranjan, by encapsulating the variables this way, you are hiding
signature details. How does any one know specifically which variables
are required for Foo()?

Also, I don't see how the interface helps. In customDataGrid, I create
an interface object. I can expose interface methods inside
customDataGrid but those methods need to come through to any one
accessing customDataGrid. This goes back to wrappers.

It would be nice if I could do:
public class
CustomDataGrid:System.Windows.Forms.DataGrid
{
public IDataGridStyleColumnCollection dcc;

And then somewhere else do:
CustomDataGrid mygrid = new CustomDataGrid()
mygrid.interfacemethod_from_dgExt();

Public members in dgExt are now exposed via customDataGrid.

Thanks,
Brett

Jan 20 '06 #5
Brett,

Well for that matter all the properties for this object should be required.
This way when you upgrade your functions, you might be able to provide backward
compatibility.

r.

Ranjan, by encapsulating the variables this way, you are hiding
signature details. How does any one know specifically which variables
are required for Foo()?

Also, I don't see how the interface helps. In customDataGrid, I
create an interface object. I can expose interface methods inside
customDataGrid but those methods need to come through to any one
accessing customDataGrid. This goes back to wrappers.

It would be nice if I could do:
public class
CustomDataGrid:System.Windows.Forms.DataGrid
{
public IDataGridStyleColumnCollection dcc;
And then somewhere else do:
CustomDataGrid mygrid = new CustomDataGrid()
mygrid.interfacemethod_from_dgExt();
Public members in dgExt are now exposed via customDataGrid.

Thanks,
Brett

Jan 20 '06 #6
that will not help the OP, he still has to map dgExt's functionality to
customDataGrid one by one. interface does not add any value to him.

"Bruce Wood" wrote:
Create an interface that defines the functionality of dgExt, and have
both dgExt and your custom DataGrid implement the interface.

In the absence of multiple inheritance, it's the best you can do.

Jan 20 '06 #7
Ranjan, what you are doing is building in many restrictions. If
methodA only needs one parameter, your saying I'll need to initialize
everything just to use methodA. Let's say I'm not at the stage to give
property Z proper data. What then? Perhaps just providing some
intellisense via comments will do it for those that are paying
attention. I think its a little dangerous though.

The method you are describing is good for backward compatibility. I've
been contemplating this with enums on some params but the class idea is
actually better.

Brett

Jan 20 '06 #8
First ask yourself is dgExt class really needed as a seperate entity? Is it
going to be reused by another class other than CustomDataGrid? If not, then
merge the functionality into CustomDataGrid.

If it is reused. What classes are going to consume it? Are they all data
grids? If yes, then create it as DataGridExtBase : DataGrid, and have
CustomDataGrid inherit from that instead.

If other non datagrid classes will use it, then you are stuck with what you
are doing now. It's essentially a utility class of sort, does not really
fall into the inheritance hierarchy of anything.

"Brett Romero" wrote:
I know inheriting from two different classes isn't allowed but here is
my situation. I'm creating a custom datagrid class that inherits from
DataGrid. I have another class (dgExt) with useful methods that will
be used in the customDataGrid() class. I can create a new dgExt
object in customDataGrid() and reference methods in dgExt. I'd
basically wrap any method I needed in dgExt and make the wrapper public
in customDataGrid. There's a scaling problem though.

If dgExt needs to change any of its method signatures, my code in
customDataGrid breaks. Inheritance probably doesn't help very much
here. However, by using wrappers, I'm having to completely copy the
parameters in the signatures of dgExt. Not exactly code reuse.

Is there a better way to do this?

Thanks,
Brett

Jan 20 '06 #9
Daniel, I think you got, "If yes, then create it as DataGridExtBase :
DataGrid, and have
CustomDataGrid inherit from that instead". It should have been obvious
to me. Nothing else can use dgExt because everything applies to
DataGrids.

Thanks,
Brett

Jan 20 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Fabian Neumann | last post: by
1 post views Thread by Jeff Schmidt | last post: by
2 posts views Thread by PEK | last post: by
4 posts views Thread by Slavyan | last post: by
6 posts views Thread by Mohammad-Reza | last post: by
3 posts views Thread by justsome_newbie | last post: by
11 posts views Thread by Chris | last post: by
reply views Thread by rosydwin | last post: by

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.