how do I get my result data set back from the constructor?

Novice somewaht in OO

I want to create a class that returns a typed dataset DataSetPastDue

So I strat with

public class PastDueTables : DataSetPastDue {

//Then the constructor takes a prameter

public PastDueTables(string PlantCode) {

and the code starts with

DataSetPastDue MyDataset = new DataSetPastDue();

and contiues to add data in the tables of the MyDataset

when done I thought I could issue

return MyDataset; but the compiler complains Error 1 Since 'WebServiceS5.PastDueTables.PastDueTables(string)' returns void, a return keyword must not be followed by an object expression

tried this=MyDataset; it tells me that this is read only.

SO how do I get my result data set back from the constructor





Answer this question

how do I get my result data set back from the constructor?

  • darklightred

    I guess any method in any class that will return the results in a dataset typed like DataSetPastDue will due.

    I was just trying to learn how to construct a class to be a knowen type.



  • Mohsen Kokabi

    You wouldn’t return the inherited type, instead you are returning an instance of the super class that inherits the base type... if you need access to only the base type portion of the class, you can simply type cast... or is the problem accessing the properties/methods/fields of the base type from the constructor or even calling the constructor of the base type from the inheriting types constructor

  • Danny Tuppeny

    The DataSetPastDue MyDataset = new DataSetPastDue(); is very strange

    The fact that your PastDueTables inherits from DataSetPastDue, the DataSetPastDue contructor will be called before your PastDueTables constructor.

    The proper way to do this is to expose a protected method in DataSetPastDue to allow add data, for instance AddRow, then, in the PastDueTables constructor, just call this.AddRow to add data.



  • dtsn

    Given that a constructor is incapable of returning anything in the same way that a regular method can, one option would be to add an extra parameter to the constructor with the out keyword, a parameter whose value would be set within the constructor and then made available externally:

    The constructor would look like:

    public class PastDueTables : DataSetPastDue

    {

    public PastDueTables(string PlantCode, out DataSetPastDue pastDue)

    {

    DataSetPastDue MyDataset = new DataSetPastDue();

    pastDue = MyDataset;

    }

    }


    While the caller would be:

    DataSetPastDue pastDueData;

    PastDueTables pdt = new PastDueTables("Some Plant", out pastDueData);

    //pastDueData now has a value;

    Are you doing anything more with the instance of the PastDueTables class after you create it If so... perhaps the gathering of the data should be pushed off to a separate method that can return the type you want.



  • ckrepps

    This ends up being the same as NOT inheretinting the class from DataSetPastDue

    My Idea was to use the class as in a line like:

    DataSetPastDue pastDueData = new PastDueTables("Some Plant");

    My Problem is that inside the class I don't know the syntax to make it return the inhereted type.



  • how do I get my result data set back from the constructor?