473,387 Members | 3,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Inherit from DataSet

I need to expand the DataSet class by inheriting from it and adding
functions that work on the data in the tables. However, since I can't
upcast how can I get my base DataSet object assigned an actual
DataSet?

e.g.

public class MyDataSet : DataSet
{
// can't do, no valid DataSet constructor
public MyDataSet(DataSet ds) : base(ds) {}

// can't do
public Set(DataSet ds) {
base = ds;
}
}

MyDataSet myDS;
// can't do, can't upcast
myDS = SqlHelper.ExecuteDataSet("My_Proc");
This seems an obvious use of inheritance. What am I missing?

Thanks,
Mike
Nov 15 '05 #1
5 5350

"Mike" <_n***@watev.com> wrote in message
news:fb**************************@posting.google.c om...
I need to expand the DataSet class by inheriting from it and adding
functions that work on the data in the tables. However, since I can't
upcast how can I get my base DataSet object assigned an actual
DataSet?

e.g.

public class MyDataSet : DataSet
{
// can't do, no valid DataSet constructor
public MyDataSet(DataSet ds) : base(ds) {}

// can't do
public Set(DataSet ds) {
base = ds;
}
}

MyDataSet myDS;
// can't do, can't upcast
myDS = SqlHelper.ExecuteDataSet("My_Proc");
This seems an obvious use of inheritance. What am I missing?

Thanks,
Mike


Hmmm I take it you're using Microsoft's data access application block? The
"regular" (pure ADO.NET) way of filling a dataset would be instead to use a
DataAdapter, which takes a DataSet as input parameter to its Fill method,
like this:

// Assume that selectCmd is a useful SqlCommand object.
MyDataSet myDS;
DataAdapter da = new DataAdapter();
da.SelectCommand = selectCmd;
da.Fill(myDS);

As you can see, here upcasting is no problem whatsoever... what you should
do in case you need to use the application block, I dunno.

Regards,
Einar
Nov 15 '05 #2
> > public class MyDataSet : DataSet
{
// can't do, no valid DataSet constructor
public MyDataSet(DataSet ds) : base(ds) {}

// can't do
public Set(DataSet ds) {
base = ds;
}
}

MyDataSet myDS;
// can't do, can't upcast
myDS = SqlHelper.ExecuteDataSet("My_Proc");
This seems an obvious use of inheritance. What am I missing?

Thanks,
Mike
Hmmm I take it you're using Microsoft's data access application block? The
"regular" (pure ADO.NET) way of filling a dataset would be instead to use

a DataAdapter, which takes a DataSet as input parameter to its Fill method,
like this:

// Assume that selectCmd is a useful SqlCommand object.
MyDataSet myDS;
DataAdapter da = new DataAdapter();
da.SelectCommand = selectCmd;
da.Fill(myDS);

As you can see, here upcasting is no problem whatsoever... what you should
do in case you need to use the application block, I dunno.


You have your inheritence confused.

Your example and that of the OP are completely opposite in assignments
(which explains exactly why yours works and the OP's throws an invalid cast
exception).

Nov 15 '05 #3
TB

----- Mike wrote: ----

I need to expand the DataSet class by inheriting from it and addin
functions that work on the data in the tables. However, since I can'
upcast how can I get my base DataSet object assigned an actua
DataSet

e.g

public class MyDataSet : DataSe

// can't do, no valid DataSet constructo
public MyDataSet(DataSet ds) : base(ds) {

// can't d
public Set(DataSet ds)
base = ds

MyDataSet myDS
// can't do, can't upcas
myDS = SqlHelper.ExecuteDataSet("My_Proc");
This seems an obvious use of inheritance. What am I missing

Hmmm..., without a copy constructor in DataSet you cannot do this. The closest you can get is by somehow trying to use DataSet.Copy() but even there you are stuck in a catch-22 trying to do this through inheritance. I don't know if there was any specific rational in not giving DataSet a copy constructor but let's assume there was. In this case I think you need to use a "Has-A" rather than an "Is-A" relationship..

public MyDataSe

private DataSet m_BaseDS

// Default constructio
public MyDataSet() { m_BaseDS = new DataSet();

// Construct a MyDataSet from an existing DataSe
public MyDataSet(DataSet ds) { m_BaseDS = ds;

// Get or Set the encapsulated DataSe
public DataSet DataSe

get { return m_BaseDS;
set { m_BaseDS = value;
// Expose/Forward methods and properties to encapsulated DataSet for convenience
// although this isn't absolutely necessary since we can get at the DataSet directly
public DataTableCollection Tables { get { return m_BaseDS.Tables; }

// and so on..
-- T
Nov 15 '05 #4
Since I really wanted to "be-a" dataset rather than "have-a" dataset I
decided on a variation of Einar's idea for the solution by adding a
static method to my DataSet decendent. For anyone googling this later
on...

public static MyDataSet Execute(string procName, params object[]
paramList)
{
MyDataSet ds = new MyDataSet();
using(SqlConnection cn = new SqlConnection(ConnectionString))
{
SqlCommand cmd = new SqlCommand(PrepareSql(procName, paramList),
cn);
using( SqlDataAdapter da = new SqlDataAdapter(cmd) ) {
cn.Open();
da.Fill(ds);
cn.Close();
return ds;
}
}
return ds;
}

which allows me to:

MyDataSet myDataSet = MyDataSet.Execute(Procedure, ParamList);

Thanks for the comments-
Mike
"TB" <tb**********@kaxy.NOSPAM.com> wrote in message news:<39**********************************@microso ft.com>...
----- Mike wrote: -----

I need to expand the DataSet class by inheriting from it and adding
functions that work on the data in the tables. However, since I can't
upcast how can I get my base DataSet object assigned an actual
DataSet?

e.g.

public class MyDataSet : DataSet
{
// can't do, no valid DataSet constructor
public MyDataSet(DataSet ds) : base(ds) {}

// can't do
public Set(DataSet ds) {
base = ds;
}
}

MyDataSet myDS;
// can't do, can't upcast
myDS = SqlHelper.ExecuteDataSet("My_Proc");
This seems an obvious use of inheritance. What am I missing?

Hmmm..., without a copy constructor in DataSet you cannot do this. The closest you can get is by somehow trying to use DataSet.Copy() but
even there you are stuck in a catch-22 trying to do this through
inheritance. I don't know if there was any specific rational in not
giving DataSet a copy constructor but let's assume there was. In this
case I think you need to use a "Has-A" rather than an "Is-A"
relationship...
public MyDataSet
{
private DataSet m_BaseDS;

// Default construction
public MyDataSet() { m_BaseDS = new DataSet(); }

// Construct a MyDataSet from an existing DataSet
public MyDataSet(DataSet ds) { m_BaseDS = ds; }

// Get or Set the encapsulated DataSet
public DataSet DataSet
{
get { return m_BaseDS; }
set { m_BaseDS = value; }
}

// Expose/Forward methods and properties to encapsulated DataSet for convenience,
// although this isn't absolutely necessary since we can get at the DataSet directly.
public DataTableCollection Tables { get { return m_BaseDS.Tables; } }

// and so on...
}

-- TB

Nov 15 '05 #5
TB


----- Mike wrote: ----

Since I really wanted to "be-a" dataset rather than "have-a" dataset
decided on a variation of Einar's idea for the solution by adding
static method to my DataSet decendent. For anyone googling this late
on..

public static MyDataSet Execute(string procName, params object[
paramList

MyDataSet ds = new MyDataSet()
using(SqlConnection cn = new SqlConnection(ConnectionString)

SqlCommand cmd = new SqlCommand(PrepareSql(procName, paramList)
cn)
using( SqlDataAdapter da = new SqlDataAdapter(cmd) )
cn.Open()
da.Fill(ds)
cn.Close()
return ds
}

return ds
[snip

Fair enough, but note that this is no solution to the problem you originally posed -- or perhaps I just misinterpreted it. You can always treat your derived class as a vanilla DataSet, fill it via a DataAdapter, etc. There is no trick here

What you cannot do, even with your solution, is accept a vanilla DataSet and somehow "bind" it to an instance of your derived class (short of tediously copying the data from it) as you showed in your original post

-- T

Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Fabian Neumann | last post by:
Hi! I got a problem with font-family inheritance. Let's say I have CSS definitions like: p { font:normal 10pt Verdana; } strong { font:normal 14pt inherit;
1
by: Jeff Schmidt | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello, ~ Ok, I've got some tables, and a style sheet that contains stuff to format the table. The problem I'm having is, it appears that using...
7
by: bobsled | last post by:
For class A to reuse functionality of a class B, A could either contains B or inherits from B. When should pick which? In a book the author says that "Don't inherit from a concrete class." Does...
4
by: Slavyan | last post by:
(I just started to learn C#.NET) What's the syntax in c# for a class to inherit more than one class. I know following syntax: public class MyClass : MyOtherClass { } but I need to inherit...
6
by: Mohammad-Reza | last post by:
I wrote a component using class library wizard. In my component i want to in order to RightToLeft property do some works. I can find out if user set this property to Yes or No, But if He/She set it...
2
by: ad | last post by:
We can inherit a class. But if the class is the code behind, when we inherit it , can we inherit it's aspx ahead together ?
7
by: Steve Barnett | last post by:
I have a database with two tables "Structure" and "Data". The Data table contains the data for my application, so no problem there. The Structure table effectively puts some organisation to the...
5
by: Redivivus | last post by:
Hi How to inherit from System.Data.DataRow? class ItemRow: DataRow { public ItemRow(DataRowBuilder rb):base(rb){} }
2
by: GTalbot | last post by:
Hello fellow comp.infosystems.www.authoring.stylesheets colleagues, Imagine this situation: #grand-parent-abs-pos { height: 400px; position: absolute; width: 600px; }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.