473,473 Members | 2,357 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How convert a base class into a subclass??

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.

Jul 21 '05 #1
5 5234
Eidolon <aa****@KILLSPAM.yahoo.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2

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.

Jon Skeet [C# MVP] wrote:
Eidolon <aa****@KILLSPAM.yahoo.com> wrote:
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.


Jul 21 '05 #3
Eidolon <aa****@KILLSPAM.yahoo.com> wrote:

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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4
> 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.

Jon Skeet [C# MVP] wrote:
Eidolon <aa****@KILLSPAM.yahoo.com> wrote:
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?


Jul 21 '05 #5
Eidolon <aa****@KILLSPAM.yahoo.com> wrote in news:OZZRY9hoDHA.2140
@TK2MSFTNGP09.phx.gbl:
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
Jul 21 '05 #6

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

Similar topics

0
by: Stephen Nesbitt | last post by:
All: Here's my implementation problem. I have a base class which has the responsibility for providing entry into the logging system. Part of the class responsibility is to ensure that lagger...
1
by: Gerry Sutton | last post by:
Hi All! I have noticed a strange behavior when using a constant identifier to initialize an instance list variable in a base class and then trying to modifying the list in subclasses by using...
0
by: cppsks | last post by:
Hello. I posted a question regarding this yesterday. I came up with the following solution but I am a little hesistant as to this solution having any side-effects that I am not aware of. The...
13
by: Hako | last post by:
I try this command: >>> import string >>> string.atoi('78',16) 120 this is 120 not 4E. Someone can tell me how to convert a decimal number to hex number? Can print A, B, C,DEF. Thank you.
2
by: Andrew G. J. Fung | last post by:
Can anyone please explain to me why the code below throws an exception? It seems to occur when deserializing an instance of a subclass, whose base class holds a struct, where said struct holds an...
8
by: Lou Pecora | last post by:
I've been scanning Python in a Nutshell, but this seems to be either undoable or so subtle that I don't know how to do it. I want to subclass a base class that is returned from a Standard Library...
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
6
by: Me | last post by:
I need to be able to acces non-virtual members of sublcasses via a base class pointer...and without the need for an explicit type cast. I thought a pure virtual getPtr() that acts as a type cast...
6
by: Bhawna | last post by:
I am into c++ code maintenance for last 3-4 years but recently I am put into design phase of a new project. Being a small comapany I dont have enough guidance from seniors. Currently I am into a...
6
by: Author | last post by:
I have class BaseClass { public BaseClass(string s1, string s2) { this.S1 = s1; this.S2 = s2; } public string S1 { get; set;}
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.