Tech Off Thread

10 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Polymorphic Typed DataSet Engine ideas?

Back to Forum: Tech Off
  • User profile image
    phreaks

    Happy Friday,

    I am trying to build a Flexible Data Access Class.

    My Goal is to new-up a Typed DataSet, pass it by ref to a DataEngine and the DataEngine will fill the typed DS.

    I thought it would be pretty straight forward in C#.

    The issue I am facing is that I can't seem to pass a Typed DS to a method that Expects a type of System.Data.DataSet.

    Anyone have any ideas and/or tips?

  • User profile image
    littleguru

    Does a typed dataset not inherit from a normal dataset?

  • User profile image
    jmbledsoe

    littleguru wrote:
    Does a typed dataset not inherit from a normal dataset?

    Typed datasets do indeed inherit from System.Data.DataSet, and it is quite possible to build a data-access engine that expects System.Data.DataSet instances, but which receives strongly-typed datasets at runtime.

    Guru may flame me for saying it Wink, but I've been involved in building one of these, which you can read about on my blog.  It sounds like the same kind of thing you're trying to do.

    If you want to post some source of the method that's failing, and the context of how it's called, that may help me diagnose the problem.

    John B.
    http://johnsbraindump.blogspot.com

  • User profile image
    jmbledsoe

    H4L0PR1CK wrote:
    My Goal is to new-up a Typed DataSet, pass it by ref to a DataEngine and the DataEngine will fill the typed DS.


    And one further note: you shouldn't need to pass the DataSet by ref to your engine to fill it.  Only pass parameters by ref if you plan on new-ing the parameter value in your method call, which I don't think you would want to do here.

    That could be related to your problem as well.

  • User profile image
    phreaks

    jmbledsoe wrote:
    
    H4L0PR1CK wrote: My Goal is to new-up a Typed DataSet, pass it by ref to a DataEngine and the DataEngine will fill the typed DS.


    And one further note: you shouldn't need to pass the DataSet by ref to your engine to fill it.  Only pass parameters by ref if you plan on new-ing the parameter value in your method call, which I don't think you would want to do here.

    That could be related to your problem as well.


    I am passing the DS by ref so that the Engine can grab the schema and determine which table(s) to fill. I thought it would be pointless to pass a value reference in and get the same type out as a return.

    So instead, I pass the DS by ref into the engine. Is there a better way?

  • User profile image
    jmbledsoe

    H4L0PR1CK wrote:
    I am passing the DS by ref so that the Engine can grab the schema and determine which table(s) to fill. I thought it would be pointless to pass a value reference in and get the same type out as a return.

    So instead, I pass the DS by ref into the engine. Is there a better way?


    You don't need a reference parameter to do what you want to do.  The only reason you would need a reference parameter is if your method assigns a new value to the parameter variable, like the following:

    public void FillDataSet(ref DataSet ds)
    {
       // Do some operations...

       ds = new DataSet();

       // Do some more operations...
    }

    Since the parameter to the method is an object, you can change the contents of the object if you pass it by value, you just can't assign a new object to the parameter variable.  The following will work fine:

    public void FillDataSet(ref DataSet ds)
    {
       // Do some operations...

       DataRow row = ds.Tables[0].NewRow();
       ds.Tables[0].AddRow(row);

       // Do some more operations...
    }

    I hope that makes sense.  I'm note sure how to explain it better, except to say try it by value first.

  • User profile image
    phreaks

    jmbledsoe wrote:
    

    H4L0PR1CK wrote: I am passing the DS by ref so that the Engine can grab the schema and determine which table(s) to fill. I thought it would be pointless to pass a value reference in and get the same type out as a return.

    So instead, I pass the DS by ref into the engine. Is there a better way?


    You don't need a reference parameter to do what you want to do.  The only reason you would need a reference parameter is if your method assigns a new value to the parameter variable, like the following:

    public void FillDataSet(ref DataSet ds)
    {
       // Do some operations...

       ds = new DataSet();

       // Do some more operations...
    }

    Since the parameter to the method is an object, you can change the contents of the object if you pass it by value, you just can't assign a new object to the parameter variable.  The following will work fine:

    public void FillDataSet(ref DataSet ds)
    {
       // Do some operations...

       DataRow row = ds.Tables[0].NewRow();
       ds.Tables[0].AddRow(row);

       // Do some more operations...
    }

    I hope that makes sense.  I'm note sure how to explain it better, except to say try it by value first.



    Ahh, I can see now.
    Thnx  =)

  • User profile image
    jmbledsoe

    Embarassed, my second example should have read as follows:

    public void FillDataSet(DataSet ds)
    {
       // Do some operations...

       DataRow row = ds.Tables[0].NewRow();
       ds.Tables[0].AddRow(row);

       // Do some more operations...
    }

    The parameter DOESN'T need to be passed by reference.  I hope that didn't confuse you!  Perplexed

  • User profile image
    littleguru

    jmbledsoe wrote:
    Guru may flame me for saying it , but I've been involved in building one of these, which you can read about on my blog.  It sounds like the same kind of thing you're trying to do.


    Hey jmbledsoe, I'm not flaming you! That's OK. If you answer and help people and point them to your thing, it's OK. Smiley

    I'm just against starting threads to do advertisement Smiley

  • User profile image
    phreaks

    jmbledsoe wrote:
    , my second example should have read as follows:

    public void FillDataSet(DataSet ds)
    {
       // Do some operations...

       DataRow row = ds.Tables[0].NewRow();
       ds.Tables[0].AddRow(row);

       // Do some more operations...
    }

    The parameter DOESN'T need to be passed by reference.  I hope that didn't confuse you! 


    Nah, I figured that was a type-o since it contradicted your premise.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.