How to inherit from two places?

B

Brett Romero

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
 
B

Bruce Wood

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

Brett Romero

In CustomDataGrid, would that look like:

public class CustomDataGrid: inheritedClass, interfaceClass

Brett
 
R

Ranjan Sakalley

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

Brett Romero

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
 
R

Ranjan Sakalley

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

Guest

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

Brett Romero

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
 
G

Guest

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

Brett Romero

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
 

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