inheritance problem
-
Hi all, I'm trying to make a class (DSWrapper) which extends DataSet, so I can put in a customized Sort() method: class DSWrapper : DataSet { public DataSet Sort() { //sorting code } } To call it I have another class (EditRecords) with a method (ListAll) that returns a DataSet, thus: DSWrapper dsw = (DSWrapper) EditRecords.ListAll(); dsw.Sort(); //...then bind to datagrid However I get a runtime exception because the cast is not valid. (Dataset -> DSWrapper) This makes sense I suppose, but is there a way round it? Is my idea just damn silly? :) If anyone could help that would be really great. Best Wishes Steven
-
Hi all, I'm trying to make a class (DSWrapper) which extends DataSet, so I can put in a customized Sort() method: class DSWrapper : DataSet { public DataSet Sort() { //sorting code } } To call it I have another class (EditRecords) with a method (ListAll) that returns a DataSet, thus: DSWrapper dsw = (DSWrapper) EditRecords.ListAll(); dsw.Sort(); //...then bind to datagrid However I get a runtime exception because the cast is not valid. (Dataset -> DSWrapper) This makes sense I suppose, but is there a way round it? Is my idea just damn silly? :) If anyone could help that would be really great. Best Wishes Steven
Hi Steven! You could add a constructor to your DSWrapper taking a DataSet as parameter:
public DSWrapper(DataSet theDataSet)
{
// store the DataSet reference somewhere
}and then perform
DSWrapper dsw = new DSWrapper(EditRecords.ListAll());
dsw.Sort();mav
-
Hi Steven! You could add a constructor to your DSWrapper taking a DataSet as parameter:
public DSWrapper(DataSet theDataSet)
{
// store the DataSet reference somewhere
}and then perform
DSWrapper dsw = new DSWrapper(EditRecords.ListAll());
dsw.Sort();mav
Hi mav, thanks for replying. Yes that works, but it means the inheritance part of it is "by the by" doesn't it? That's a "has a" rather than "is a" class. Is there a way to do it with inheritance? Steven
-
Hi mav, thanks for replying. Yes that works, but it means the inheritance part of it is "by the by" doesn't it? That's a "has a" rather than "is a" class. Is there a way to do it with inheritance? Steven
You could also alter your EditRecords class to return a DSWrapper object instead of a DataSet, but honestly I wouldn't. You could, however, leave the interface of EdtRecords.ListAll untouched and just return a DSWrapper object. DSWrapper inherits DataSet, so this should work. Then you _could_ downcast to DSWrapper and call Sort, but that's not very obvious from a design point of view. I think it's much cleaner to instantiate a subclass. Your DSWrapper class adds new functionality to DataSet, so this functionality only can be used if the object itself _is_ an instance of DSWrapper. No way to alter DataSet itself to add a Sort method. Regards, mav
-
You could also alter your EditRecords class to return a DSWrapper object instead of a DataSet, but honestly I wouldn't. You could, however, leave the interface of EdtRecords.ListAll untouched and just return a DSWrapper object. DSWrapper inherits DataSet, so this should work. Then you _could_ downcast to DSWrapper and call Sort, but that's not very obvious from a design point of view. I think it's much cleaner to instantiate a subclass. Your DSWrapper class adds new functionality to DataSet, so this functionality only can be used if the object itself _is_ an instance of DSWrapper. No way to alter DataSet itself to add a Sort method. Regards, mav
Thanks again mav I agree - it wasn't a good idea. I'll stick with the standalone class using a DataSet instance, rather than inheriting. Cheers Steven