继承基类 - 无效的强制转换异常 [英] Inheriting a base class - Invalid Cast Exception

查看:103
本文介绍了继承基类 - 无效的强制转换异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一组基类和子类,以便在我正在开发的

程序中使用。基类表示通用的查找

表。来自我的数据库,其中包含诸如汽车制造商,制造商,模式等商品列表。我创建了一个通用的

" datacollection"类和通用的数据对象 class表示

表和该表中的行作为对象的集合,具有

用于操作字段,添加行等的通用属性。


我遇到的问题是我有一个通用的项目。

DataCollection类中的方法,它接受一个关键参数,然后在数据表中查找它,并将信息作为DataObject返回。类。我

然后写了子类来更具体地使用基类来获取特定类型的数据(在我提供的示例中,一辆车

制造商)。所以我现在有一个制造商继承自

DataCollection和制造商的对象继承自

DataObject的对象。所以,回到我的问题。在item方法中,通用的

DataCollection类需要一个Key类。将用于查找。

然后搜索其内部数据表并返回相应的

DataObject对象。但是,我想要的是在我的制造商对象中创建一个重写的项目

对象,它返回一个MANUFACTURER而不是一个DATAOBJECT
。所以我创建了一个Public Shadows属性,该属性返回一个

制造商对象,但是调用MyBase.Item函数来获取它。但是

MyBase.Item返回一个DataObject,而不是一个objManufactuer,然后我得到

抛出我的Invalid Cast异常。


我希望这能解释我的困境。有人可以帮忙吗?以下是基本知识

我的代码:

公共类objDataCollection


公共只读属性项(byval Key)作为对象)

objDataObject


获取


...


返回新的objDataObject(...)


结束获取


结束物业


结束班


公共类objDataObject


..


结束班


公共类objManufacturers


继承objDataCollection


公共阴影只读属性项(byval Code as String)

as objManufacturer


获取


返回MyBase.Item(代码)


结束获取


结束物业


结束课程


公共课程objManufacturer

继承objDataObject


..


结束班


>

解决方案

通过使用阴影,你打破了多态性。你有什么理由不能使用覆盖物吗?

" Chet Cromer" < CH ******* @ ccrtc.com>在消息中写道

news:e0 ************** @ TK2MSFTNGP14.phx.gbl ...

我正在创建一个集合在我正在开发的整个
程序中使用的基类和子类。基类表示来自我的数据库的通用查找表,其包含诸如汽车的制造商,品牌,
模式等的列表。我创建了一个通用的数据收集 class和
通用dataobject class表示表和表
表中的行,作为具有通用属性的对象集合,用于操作
字段,添加行等。

问题我是我有一个通用的项目。采用关键参数然后在数据表中查找并将信息作为DataObject返回的DataCollection类中的方法。类。我已经编写了子类来更具体地使用基类来获取特定类型的数据(在我提供的示例中,是一个车辆制造商)。所以我现在有一个制造商继承自DataCollection和制造商的对象。从DataObject继承的对象。
所以,回到我的问题。在item方法中,通用DataCollection
类需要一个Key键。将用于查找。然后它搜索其内部数据表并返回相应的DataObject对象。
然而,我想要的是在我的
制造商对象中创建一个重写的Item对象,它返回一个MANUFACTURER而不是一个DATAOBJECT。
所以我创建了一个Public Shadows属性,它返回一个Manufacturer对象,但是调用MyBase.Item函数来获取它。但MyBase.Item返回一个
DataObject,而不是objManufactuer,然后我抛出了我的Invalid Cast
异常。

我希望这能解释我的困境。有人可以帮忙吗?以下是我的代码的基础知识:

Public Class objDataCollection

公共Readonly属性项(byval Key as Object)为
objDataObject

获取



返回新的objDataObject(...)

结束获取
结束财产

结束班级


公共类objDataObject



结束类

公共类objManufacturers

继承objDataCollection

公共阴影只读属性项(byval Code as String)为
objManufacturer


返回MyBase.Item(代码)

结束获取

结束财产

结束班级


公共班级objManufacturer

继承它的objDataObject







< blockquote>我之前的回复并没有真正解决你的问题。你是否仍需要CType结果,因为你要通过从基类中询问它来获取通用的

对象。

切特克罗默 < CH ******* @ ccrtc.com>在消息中写道

news:e0 ************** @ TK2MSFTNGP14.phx.gbl ...

我正在创建一个集合在我正在开发的整个
程序中使用的基类和子类。基类表示来自我的数据库的通用查找表,其包含诸如汽车的制造商,品牌,
模式等的列表。我创建了一个通用的数据收集 class和
通用dataobject class表示表和表
表中的行,作为具有通用属性的对象集合,用于操作
字段,添加行等。

问题我是我有一个通用的项目。采用关键参数然后在数据表中查找并将信息作为DataObject返回的DataCollection类中的方法。类。我已经编写了子类来更具体地使用基类来获取特定类型的数据(在我提供的示例中,是一个车辆制造商)。所以我现在有一个制造商继承自DataCollection和制造商的对象。从DataObject继承的对象。
所以,回到我的问题。在item方法中,通用DataCollection
类需要一个Key键。将用于查找。然后它搜索其内部数据表并返回相应的DataObject对象。
然而,我想要的是在我的
制造商对象中创建一个重写的Item对象,它返回一个MANUFACTURER而不是一个DATAOBJECT。
所以我创建了一个Public Shadows属性,它返回一个Manufacturer对象,但是调用MyBase.Item函数来获取它。但MyBase.Item返回一个
DataObject,而不是objManufactuer,然后我抛出了我的Invalid Cast
异常。

我希望这能解释我的困境。有人可以帮忙吗?以下是我的代码的基础知识:

Public Class objDataCollection

公共Readonly属性项(byval Key as Object)为
objDataObject

获取



返回新的objDataObject(...)

结束获取
结束财产

结束班级


公共类objDataObject



结束类

公共类objManufacturers

继承objDataCollection

公共阴影只读属性项(byval Code as String)为
objManufacturer


返回MyBase.Item(代码)

结束获取

结束财产

结束班级


公共班级objManufacturer

继承它的objDataObject







< blockquote>从头开始,你不能为它的派生类型输入一个基类。你必须

覆盖派生类中的Item属性并实现它以返回

你想要返回的类型。你不能从一个

基类中返回一个特定类型(或者更多

,特别是不能返回几种不同的特定类型)。

" Rick Mogstad <日********** @ GMail.NOSPAM.com>在消息中写道

新闻:Oa ************** @ TK2MSFTNGP14.phx.gbl ...

我以前的回复并不是真的解决你的问题。您仍然需要对结果进行CType,因为您要求从基类请求它来获得
通用对象。

Chet Cromer < CH ******* @ ccrtc.com>在消息中写道
新闻:e0 ************** @ TK2MSFTNGP14.phx.gbl ...

我正在创建一组基类和我正在开发的整个
程序中使用的子类。基类表示来自我的数据库的通用查找表,其包含诸如汽车的制造商,品牌,
模式等的列表。我创建了一个通用的数据收集 class和
通用dataobject class表示表和表
表中的行,作为具有通用属性的对象集合,用于操作
字段,添加行等。

问题我是我有一个通用的项目。采用关键参数然后在数据表中查找并将信息作为DataObject返回的DataCollection类中的方法。类。我已经编写了子类来更具体地使用基类来获取特定类型的数据(在我提供的示例中,是一个车辆制造商)。所以我现在有一个制造商继承自DataCollection和制造商的对象。从DataObject继承的对象。
所以,回到我的问题。在item方法中,通用DataCollection
类需要一个Key键。将用于查找。然后它搜索其内部数据表并返回相应的DataObject对象。
然而,我想要的是在我的
制造商对象中创建一个重写的Item对象,它返回一个MANUFACTURER而不是一个DATAOBJECT。
所以我创建了一个Public Shadows属性,它返回一个Manufacturer对象,但是调用MyBase.Item函数来获取它。但MyBase.Item返回一个
DataObject,而不是objManufactuer,然后我抛出了我的Invalid Cast
异常。

我希望这能解释我的困境。有人可以帮忙吗?以下是我的代码的基础知识:

Public Class objDataCollection

公共Readonly属性项(byval Key as Object)为
objDataObject

获取



返回新的objDataObject(...)

结束获取
结束财产

结束班级


公共类objDataObject



结束类

公共类objManufacturers

继承objDataCollection

公共阴影只读属性项(byval Code as String)为
objManufacturer


返回MyBase.Item(代码)

结束获取

结束财产

结束班级


公共班级objManufacturer

继承它的objDataObject








I am creating a set of base classes and sub classes to use throughout a
program I''m developing. The base class represents a generic "lookup
table" from my database that contains lists of things like
manufacturers, makes, modes, etc. of cars. I have created a generic
"datacollection" class and a generic "dataobject" class to represent the
table and the rows within that table as a collection of objects with
generic properties for manipulating fields, adding rows, etc.

The problem I''m having is that I have a generic "Item" method in the
DataCollection class that takes a key parameter and then looks it up in
the data table and returns the information as a "DataObject" class. I
have then written subclasses to more specifically use the base class for
a particular type of data (in the example I''m providing, a vehicle
manufacturer). So I now have a "Manufacturers" object that inherits from
DataCollection and a "Manufacturer" object that inherits from
DataObject. So, back to my problem. In the item method, the generic
DataCollection class requires a "Key" that will be used for the lookup.
It then searches its internal data table and returns the appropriate
DataObject object. However, what I WANT, is to create an overriding Item
object in my Manufacturers object that returns a MANUFACTURER instead of
a DATAOBJECT. So I created a Public Shadows property that returns a
Manufacturer object but callse the MyBase.Item function to get it. But
MyBase.Item returns a DataObject, not a objManufactuer, and then I get
thrown my Invalid Cast Exception.

I hope that explains my predicament. Can anyone help? Here is the basics
of my code:

Public Class objDataCollection

Public Readonly Property Item (byval Key as Object) as
objDataObject

Get

...

Return new objDataObject(...)

End Get

End Property

End Class

Public Class objDataObject

..

End Class

Public Class objManufacturers

Inherits objDataCollection

Public Shadows Readonly Property Item(byval Code as String)
as objManufacturer

Get

return MyBase.Item(code)

End Get

End Property

End Class

Public Class objManufacturer

Inherits objDataObject

..

End Class


解决方案

by using shadows, you are breaking polymorphism. Is there a reason you cant
use overrides?
"Chet Cromer" <ch*******@ccrtc.com> wrote in message
news:e0**************@TK2MSFTNGP14.phx.gbl...

I am creating a set of base classes and sub classes to use throughout a
program I''m developing. The base class represents a generic "lookup table"
from my database that contains lists of things like manufacturers, makes,
modes, etc. of cars. I have created a generic "datacollection" class and a
generic "dataobject" class to represent the table and the rows within that
table as a collection of objects with generic properties for manipulating
fields, adding rows, etc.

The problem I''m having is that I have a generic "Item" method in the
DataCollection class that takes a key parameter and then looks it up in
the data table and returns the information as a "DataObject" class. I have
then written subclasses to more specifically use the base class for a
particular type of data (in the example I''m providing, a vehicle
manufacturer). So I now have a "Manufacturers" object that inherits from
DataCollection and a "Manufacturer" object that inherits from DataObject.
So, back to my problem. In the item method, the generic DataCollection
class requires a "Key" that will be used for the lookup. It then searches
its internal data table and returns the appropriate DataObject object.
However, what I WANT, is to create an overriding Item object in my
Manufacturers object that returns a MANUFACTURER instead of a DATAOBJECT.
So I created a Public Shadows property that returns a Manufacturer object
but callse the MyBase.Item function to get it. But MyBase.Item returns a
DataObject, not a objManufactuer, and then I get thrown my Invalid Cast
Exception.

I hope that explains my predicament. Can anyone help? Here is the basics
of my code:

Public Class objDataCollection

Public Readonly Property Item (byval Key as Object) as
objDataObject

Get

...

Return new objDataObject(...)

End Get

End Property

End Class

Public Class objDataObject

.

End Class

Public Class objManufacturers

Inherits objDataCollection

Public Shadows Readonly Property Item(byval Code as String) as
objManufacturer

Get

return MyBase.Item(code)

End Get

End Property

End Class

Public Class objManufacturer

Inherits objDataObject

.

End Class




My previous response doesnt really address your question though. You are
still going to have to CType the result because you are ASKING for a generic
object by asking for it from the base class.
"Chet Cromer" <ch*******@ccrtc.com> wrote in message
news:e0**************@TK2MSFTNGP14.phx.gbl...

I am creating a set of base classes and sub classes to use throughout a
program I''m developing. The base class represents a generic "lookup table"
from my database that contains lists of things like manufacturers, makes,
modes, etc. of cars. I have created a generic "datacollection" class and a
generic "dataobject" class to represent the table and the rows within that
table as a collection of objects with generic properties for manipulating
fields, adding rows, etc.

The problem I''m having is that I have a generic "Item" method in the
DataCollection class that takes a key parameter and then looks it up in
the data table and returns the information as a "DataObject" class. I have
then written subclasses to more specifically use the base class for a
particular type of data (in the example I''m providing, a vehicle
manufacturer). So I now have a "Manufacturers" object that inherits from
DataCollection and a "Manufacturer" object that inherits from DataObject.
So, back to my problem. In the item method, the generic DataCollection
class requires a "Key" that will be used for the lookup. It then searches
its internal data table and returns the appropriate DataObject object.
However, what I WANT, is to create an overriding Item object in my
Manufacturers object that returns a MANUFACTURER instead of a DATAOBJECT.
So I created a Public Shadows property that returns a Manufacturer object
but callse the MyBase.Item function to get it. But MyBase.Item returns a
DataObject, not a objManufactuer, and then I get thrown my Invalid Cast
Exception.

I hope that explains my predicament. Can anyone help? Here is the basics
of my code:

Public Class objDataCollection

Public Readonly Property Item (byval Key as Object) as
objDataObject

Get

...

Return new objDataObject(...)

End Get

End Property

End Class

Public Class objDataObject

.

End Class

Public Class objManufacturers

Inherits objDataCollection

Public Shadows Readonly Property Item(byval Code as String) as
objManufacturer

Get

return MyBase.Item(code)

End Get

End Property

End Class

Public Class objManufacturer

Inherits objDataObject

.

End Class




Scratch that, you can''t ctype a base class to its derived type. You have to
override the Item property in the derived class and implement it to return
the type you wanted to return. You cant return a speficic type (or more
specifically, cant return several different specific types) from a single
base class.
"Rick Mogstad" <Ri**********@GMail.NOSPAM.com> wrote in message
news:Oa**************@TK2MSFTNGP14.phx.gbl...

My previous response doesnt really address your question though. You are
still going to have to CType the result because you are ASKING for a
generic object by asking for it from the base class.
"Chet Cromer" <ch*******@ccrtc.com> wrote in message
news:e0**************@TK2MSFTNGP14.phx.gbl...

I am creating a set of base classes and sub classes to use throughout a
program I''m developing. The base class represents a generic "lookup table"
from my database that contains lists of things like manufacturers, makes,
modes, etc. of cars. I have created a generic "datacollection" class and a
generic "dataobject" class to represent the table and the rows within that
table as a collection of objects with generic properties for manipulating
fields, adding rows, etc.

The problem I''m having is that I have a generic "Item" method in the
DataCollection class that takes a key parameter and then looks it up in
the data table and returns the information as a "DataObject" class. I
have then written subclasses to more specifically use the base class for
a particular type of data (in the example I''m providing, a vehicle
manufacturer). So I now have a "Manufacturers" object that inherits from
DataCollection and a "Manufacturer" object that inherits from DataObject.
So, back to my problem. In the item method, the generic DataCollection
class requires a "Key" that will be used for the lookup. It then searches
its internal data table and returns the appropriate DataObject object.
However, what I WANT, is to create an overriding Item object in my
Manufacturers object that returns a MANUFACTURER instead of a DATAOBJECT.
So I created a Public Shadows property that returns a Manufacturer object
but callse the MyBase.Item function to get it. But MyBase.Item returns a
DataObject, not a objManufactuer, and then I get thrown my Invalid Cast
Exception.

I hope that explains my predicament. Can anyone help? Here is the basics
of my code:

Public Class objDataCollection

Public Readonly Property Item (byval Key as Object) as
objDataObject

Get

...

Return new objDataObject(...)

End Get

End Property

End Class

Public Class objDataObject

.

End Class

Public Class objManufacturers

Inherits objDataCollection

Public Shadows Readonly Property Item(byval Code as String) as
objManufacturer

Get

return MyBase.Item(code)

End Get

End Property

End Class

Public Class objManufacturer

Inherits objDataObject

.

End Class





这篇关于继承基类 - 无效的强制转换异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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