Inheriting System.Data.DataTable issue

  • Thread starter Thread starter Rory Plaire
  • Start date Start date
R

Rory Plaire

Greetings all,

I am (was, rather -- I have since went the route of containment to
implement the pattern) trying to inherit DataTable to construct a Lazy
Load patterned table for large result sets.

However, I kept getting an error when adding new rows... exceptions
were thrown declaring the row to have already been added to the
collection, however, if I didn't call DataTable.Rows.Add, the
exception was, of course, that I hadn't added the row to the table...

The structure was simple: override (actually, hide) the Rows property
on the descended table class and return a wrapper to the underlying
DataRowsCollection class of the base table, one which would catch
accesses to rows which were not yet loaded.

The problem came in trying to add new rows through the wrapper... it
seems like it couldn't be done. I tried to disassemble DataTable in
..Net Reflector to see which code was setting the row membership, but
could discern it. I chalked it up to a flaw in the design (since the
base behavior was contradictory), but would love to hear if anyone has
more insight into this matter.


thanks,
-rory 8)
 
Rory Plaire said:
Greetings all,

I am (was, rather -- I have since went the route of containment to
implement the pattern) trying to inherit DataTable to construct a Lazy
Load patterned table for large result sets.

However, I kept getting an error when adding new rows... exceptions
were thrown declaring the row to have already been added to the
collection, however, if I didn't call DataTable.Rows.Add, the
exception was, of course, that I hadn't added the row to the table...

The structure was simple: override (actually, hide) the Rows property
on the descended table class and return a wrapper to the underlying
DataRowsCollection class of the base table, one which would catch
accesses to rows which were not yet loaded.

DataTable.Rows is not virtual. This means that you may be using your new
Rows property, but the anyone with a DataTable reference is referencing the
DataTable's Rows property.
 
Hi John,

I was hiding, not overriding, the Rows property.

public new PagedRowCollection Rows
{
...
}

This is a way to mask a property not marked with "virtual" on an
inherited class in C#.

kindly,
-rory 8)
 
rplaire said:
Hi John,

I was hiding, not overriding, the Rows property.

public new PagedRowCollection Rows
{
..
}

This is a way to mask a property not marked with "virtual" on an
inherited class in C#.

Yes, but it has a side-effect you may not be aware of. If you have:

YourTable t = GetYourTable();
int i = t.Rows.Count;

In the above, you will be calling your Rows method. However,

DataTable t = GetYourTable();
int i = t.Rows.Count;

In the above, you will be calling the DataTable version of Rows - the one
you hid. This will also happen when you pass an instance of YourTable to any
method which expects an argument of type DataTable.

The symptom you mentioned sounded like it could be caused by some code
calling your Rows property and other code calling the DataTable Rows
property.
 
Back
Top