compile error when DataRow used as base class

S

Steve Richter

here is a simple class:

public class Demo1 : System.Data.DataRow
{
public Demo1()
{
}
}

but it does not compile:
C:\src#\Steve\Demo1.cs(10): No overload for method 'DataRow'
takes '0' arguments

Am I not able to subclass the DataRow class?

thanks,
-Steve
 
G

Guest

Hi Steve,

The constructor for a DataRow is defined as:

protected internal DataRow( DataRowBuilder builder );

Change your class to say:

using System;
using System.Data;
....
public class Demo1 : DataRow
{
public Demo1( DataRowBuilder builder ) : base( builder )
{
}
}
....

Daryush
 
J

Jon Skeet [C# MVP]

Steve Richter said:
here is a simple class:

public class Demo1 : System.Data.DataRow
{
public Demo1()
{
}
}

but it does not compile:
C:\src#\Steve\Demo1.cs(10): No overload for method 'DataRow'
takes '0' arguments

Am I not able to subclass the DataRow class?

See http://www.pobox.com/~skeet/csharp/constructors.html

Your code is equivalent to:

public class Demo1 : System.Data.DataRow
{
public Demo1() : base()
{
}
}

and as there isn't a parameterless constructor available, you get a
compiler error.
 
S

Steve Richter

Daryush said:
Hi Steve,

The constructor for a DataRow is defined as:

protected internal DataRow( DataRowBuilder builder );

Change your class to say:

using System;
using System.Data;
...
public class Demo1 : DataRow
{
public Demo1( DataRowBuilder builder ) : base( builder )
{
}
}

great! thank you.

Now the big question, a brief answer from anyone will be ok with me,:
I can subclass DataTable
class MyTable : DataTable
{
}

and I can subclass DataRow
class MyRow : DataRow
{
public MyRow( DataRowBuilder builder ) : base( builder )
{
}
}

how do I program MyTable.NewRow( ) to return a "MyRow" object instead
of a "DataRow" object?

-Steve
 
J

Jon Skeet [C# MVP]

Steve Richter said:
great! thank you.

Now the big question, a brief answer from anyone will be ok with me,:
I can subclass DataTable
class MyTable : DataTable
{
}

and I can subclass DataRow
class MyRow : DataRow
{
public MyRow( DataRowBuilder builder ) : base( builder )
{
}
}

how do I program MyTable.NewRow( ) to return a "MyRow" object instead
of a "DataRow" object?

I believe the right thing to do is override NewRowFromBuilder to return
the appropriate type of row, and NewRow will call NewRowFromBuilder
with the appropriate DataRowBuilder. I've never done it myself
though...

You might want to get VS.NET to generate a strongly typed data set
(with any old schema) and have a look at what the generated code looks
like - that should give you a reasonable idea of what's required.
 
S

Steve Richter

Jon said:
I believe the right thing to do is override NewRowFromBuilder to return
the appropriate type of row, and NewRow will call NewRowFromBuilder
with the appropriate DataRowBuilder. I've never done it myself
though...

You might want to get VS.NET to generate a strongly typed data set
(with any old schema) and have a look at what the generated code looks
like - that should give you a reasonable idea of what's required.

thank you, Jon.

What I am looking to do is have a property in MyRow for each column in
the table. Using an indexer to get and set objects in the
DataRowCollection seems nuts to me, too much hardcoding of names.

I would much prefer:
row.EmpID = 25 ;

over
row["EmpID"] = 25 ;

-Steve
 
J

Jon Skeet [C# MVP]

Steve Richter said:
What I am looking to do is have a property in MyRow for each column in
the table. Using an indexer to get and set objects in the
DataRowCollection seems nuts to me, too much hardcoding of names.

I would much prefer:
row.EmpID = 25 ;

over
row["EmpID"] = 25 ;

You might want to look at getting VS.NET to generate a strongly typed
dataset for you, unless you have a particular reason to write all the
code yourself - it would at least be a good starting point.

(Using non-strongly-typed datasets isn't too bad so long as you don't
put the literals in the code, but use constants defined elsewhere - or
even better, fetch the DataColumn and use that, as it would be more
efficient.)
 

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