How convert a base class into a subclass??

  • Thread starter Thread starter Eidolon
  • Start date Start date
E

Eidolon

I have a class which inherits from System.Data.DataTable. The code is
basically (property procs omitted for clarity):

Public Class DataTable()
Inherits System.Data.DataTable

Public Property SQL As String = ""
End Class

Now, i have a proc which given a SQL returns the System.Data.DataTable
from execution. I am trying to use this as follows (code simplified for
clarity):

Dim dt As MYLIB.DataTable, sSQL As String = "SELECT * FROM MYTABLE"
dt = MyProc(sSQL)
dt.SQL = sSQL

When run, it tells me "Specified cast is invalid." Why? my
MYLIB.DataTable class inherits from System.Data.DataTable. This is a
widening conversion, so what is the problem?

Thanks in advance,
- Aaron.
 
Eidolon said:
I have a class which inherits from System.Data.DataTable. The code is
basically (property procs omitted for clarity):

Public Class DataTable()
Inherits System.Data.DataTable

Public Property SQL As String = ""
End Class

Now, i have a proc which given a SQL returns the System.Data.DataTable
from execution. I am trying to use this as follows (code simplified for
clarity):

Dim dt As MYLIB.DataTable, sSQL As String = "SELECT * FROM MYTABLE"
dt = MyProc(sSQL)
dt.SQL = sSQL

When run, it tells me "Specified cast is invalid." Why? my
MYLIB.DataTable class inherits from System.Data.DataTable. This is a
widening conversion, so what is the problem?

The problem is that you can't pretend that something is a more derived
type than it is. It's missing a load of fields, potentially! Consider
if you could cast a plain Object to FileStream - what would that mean?
Or how about Object to String?

You'll need to either not need dt to be a MYLIB.DataTable, or write
some conversion from a System.Data.DataTable to a MYLIB.DataTable.
 
Thanks for the expo, the Obj-->FS example cleared it up for me,
conceptually.

Now though, how do you go about writing a converter for something
like this? I tried putting a constructor on MYLIB.DataTable as such:

Public Sub New(dt As System.Data.DataTable)
MyBase = dt.Copy()
End Sub

But it tells me that MyBase is a value, and cannot be assigned. So how
would i make a conversion? Do i have to manually code to go through
every single property of the S.D.DT, and each column, and row, and
manually copy them over onto MYLIB.DT? There is no way to update MyBase
to be the new S.D.DT?
C# code would be fine too, if you have an example, ill be able to
convert it to VB.

Thanks for your help,
- Aaron.
 
Eidolon said:
Thanks for the expo, the Obj-->FS example cleared it up for me,
conceptually.

Now though, how do you go about writing a converter for something
like this? I tried putting a constructor on MYLIB.DataTable as such:

Public Sub New(dt As System.Data.DataTable)
MyBase = dt.Copy()
End Sub

But it tells me that MyBase is a value, and cannot be assigned. So how
would i make a conversion? Do i have to manually code to go through
every single property of the S.D.DT, and each column, and row, and
manually copy them over onto MYLIB.DT?

Yes, basically. This is rarely good design in general though.
There is no way to update MyBase to be the new S.D.DT?

No.

What does MYLIB.DataTable give you over the normal one? Could you make
it a composite type which doesn't actually derive from
System.Data.DataTable but *contains* one?
 
What does MYLIB.DataTable give you over the normal one? Could you make
it a composite type which doesn't actually derive from
System.Data.DataTable but *contains* one?

Basically i just wanted a way to be able to extend it and add on
whatever we wanted. The particular thing i was looking for here was an
SQL property where after executing a query, and getting back a DT, i
could stick the relevant SQL in there for retrieval later, for debugging
and the like. Ive been just sticking it in the Namespace property of the
DT. I guess ill just stick with that for now.

Thanks for your help anyway.
- Aaron.
 
I have a class which inherits from System.Data.DataTable. The code is
basically (property procs omitted for clarity):

Public Class DataTable()
Inherits System.Data.DataTable

Public Property SQL As String = ""
End Class

Now, i have a proc which given a SQL returns the System.Data.DataTable
from execution. I am trying to use this as follows (code simplified for
clarity):

Dim dt As MYLIB.DataTable, sSQL As String = "SELECT * FROM MYTABLE"
dt = MyProc(sSQL)
dt.SQL = sSQL

When run, it tells me "Specified cast is invalid." Why? my
MYLIB.DataTable class inherits from System.Data.DataTable. This is a
widening conversion, so what is the problem?

Thanks in advance,
- Aaron.

Use Containment instead of Inheritance...

================================================
public class MyDataClass
{
private System.Data.DataTable _dt;
private string _sql;
public void MyDataClass(string sql)
{ //constructor
_sql = sql; _dt = null;
}
public System.Data.DataTable Table
{
get
{
if (_dt == null)
{
//create your datatable
}
return _dt;
}
}
public string SQL
{
get{return _sql;}
set{_sql = value; _dt = null;}
}
}
================================================

Michael Lang, MCSD
 
Back
Top