如何将基类转换为子类? [英] How convert a base class into a subclass??

查看:166
本文介绍了如何将基类转换为子类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个继承自System.Data.DataTable的类。代码基本上是

(为清晰起见省略了属性过程):

公共类DataTable()

继承System.Data。 DataTable


公共属性SQL As String =""

End Class


现在,我有一个给出SQL的proc从执行中返回System.Data.DataTable

。我试图使用如下(代码简化为

清晰度):


Dim dt作为MYLIB.DataTable,sSQL As String =" SELECT *来自MYTABLE"

dt = MyProc(sSQL)

dt.SQL = sSQL


运行时,它告诉我指定的演员表无效。为什么?我的

MYLIB.DataTable类继承自System.Data.DataTable。这是一个

扩大转换,那么问题是什么?


提前谢谢,

- Aaron。

解决方案

Eidolon< aa **** @ KILLSPAM.yahoo.com>写道:

我有一个继承自System.Data.DataTable的类。代码基本上是(为了清楚起见省略了属性过程):

Public Class DataTable()
继承System.Data.DataTable

公共属性SQL As String =""
End Class

现在,我有一个proc,它给出了一个SQL,从执行中返回System.Data.DataTable
。我试图使用如下(代码简化为
清晰度):

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

运行时,它告诉我指定的演员表无效。为什么?我的
MYLIB.DataTable类继承自System.Data.DataTable。这是一个扩大的转换,所以问题是什么?




问题是你不能假装某些东西是更有衍生的东西/>
类型比它。它潜在地缺少了大量的田地!如果你可以将一个普通的对象转换为FileStream,那么考虑

- 这意味着什么?

或者Object to String怎么样?


你需要或者不需要dt成为MYLIB.DataTable,或者写一下从b。System.DataTable到MYLIB.DataTable的转换。

-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该群组,请不要给我发邮件。 />




感谢世博会,Obj - > FS示例为我清理了它,

概念上。


现在,你如何为这样的东西写一个转换器

?我尝试在MYLIB.DataTable上放置一个构造函数:

Public Sub New(dt As System.Data.DataTable)

MyBase = dt.Copy( )

结束子


但它告诉我MyBase是一个值,无法分配。那么我将如何转换
?我是否必须手动编码以通过

S.D.DT的每个属性,每列和行,以及

手动将它们复制到MYLIB.DT上?有没有办法更新MyBase

成为新的SDDT?

C#代码也没关系,如果你有一个例子,我可以

将其转换为VB。


感谢您的帮助,

- Aaron。


Jon Skeet [C#MVP]写道:

Eidolon< aa **** @ KILLSPAM.yahoo.com>写道:

我有一个继承自System.Data.DataTable的类。代码基本上是(为了清楚起见省略了属性过程):

Public Class DataTable()
继承System.Data.DataTable

公共属性SQL As String =""
End Class

现在,我有一个proc,它给出了一个SQL,从执行中返回System.Data.DataTable
。我试图使用如下(代码简化为
清晰度):

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

运行时,它告诉我指定的演员表无效。为什么?我的
MYLIB.DataTable类继承自System.Data.DataTable。这是一个扩大的转换,所以问题是什么?



问题是你不能假装某些东西是比派生的更多的类型而不是它是。它潜在地缺少了大量的田地!考虑
如果你可以将一个普通的对象转换为FileStream - 这意味着什么?
或者Object to String怎么样?

你需要不需要dt来是一个MYLIB.DataTable,或写一些从System.Data.DataTable到MYLIB.DataTable的转换。




Eidolon< aa **** @ KILLSPAM.yahoo.com>写道:


感谢世博会,Obj - > FS示例为我清理了它,概念上。

现在,你是怎么为这样的东西写一个转换器的?我尝试在MYLIB.DataTable上放置一个构造函数:

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

但是它告诉我MyBase是一个值,并且无法分配。那么我将如何进行转换呢?我是否必须手动编码才能完成S.D.DT的每一个属性,以及每一列和一行,并手动将它们复制到MYLIB.DT上?


是的,基本上。一般来说这很少是好的设计。

有没有办法更新MyBase成为新的SDDT?




No.


MYLIB.DataTable给你的正常价值是什么?你能不能把它变成一个复合类型,它实际上并不是来自

System.Data.DataTable但*包含*一个?


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该群组,请不要给我发邮件


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 <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



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.




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


这篇关于如何将基类转换为子类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆