结束财产 结束财产 结束财产 返回MyBase.Item(代码) 结束获取 结束财产 结束班级 公共班级objManufacturer 继承它的objDataObject 。 I am creating a set of base classes and sub classes to use throughout aprogram I''m developing. The base class represents a generic "lookuptable" from my database that contains lists of things likemanufacturers, makes, modes, etc. of cars. I have created a generic"datacollection" class and a generic "dataobject" class to represent thetable and the rows within that table as a collection of objects withgeneric properties for manipulating fields, adding rows, etc.The problem I''m having is that I have a generic "Item" method in theDataCollection class that takes a key parameter and then looks it up inthe data table and returns the information as a "DataObject" class. Ihave then written subclasses to more specifically use the base class fora particular type of data (in the example I''m providing, a vehiclemanufacturer). So I now have a "Manufacturers" object that inherits fromDataCollection and a "Manufacturer" object that inherits fromDataObject. So, back to my problem. In the item method, the genericDataCollection class requires a "Key" that will be used for the lookup.It then searches its internal data table and returns the appropriateDataObject object. However, what I WANT, is to create an overriding Itemobject in my Manufacturers object that returns a MANUFACTURER instead ofa DATAOBJECT. So I created a Public Shadows property that returns aManufacturer object but callse the MyBase.Item function to get it. ButMyBase.Item returns a DataObject, not a objManufactuer, and then I getthrown my Invalid Cast Exception.I hope that explains my predicament. Can anyone help? Here is the basicsof my code:Public Class objDataCollectionPublic Readonly Property Item (byval Key as Object) asobjDataObjectGet...Return new objDataObject(...)End GetEnd PropertyEnd ClassPublic Class objDataObject..End ClassPublic Class objManufacturersInherits objDataCollectionPublic Shadows Readonly Property Item(byval Code as String)as objManufacturerGetreturn MyBase.Item(code)End GetEnd PropertyEnd ClassPublic Class objManufacturerInherits objDataObject..End Class 解决方案 by using shadows, you are breaking polymorphism. Is there a reason you cantuse overrides?"Chet Cromer" <ch*******@ccrtc.com> wrote in messagenews:e0**************@TK2MSFTNGP14.phx.gbl...I am creating a set of base classes and sub classes to use throughout aprogram 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 ageneric "dataobject" class to represent the table and the rows within thattable as a collection of objects with generic properties for manipulatingfields, 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 arestill going to have to CType the result because you are ASKING for a genericobject by asking for it from the base class."Chet Cromer" <ch*******@ccrtc.com> wrote in messagenews:e0**************@TK2MSFTNGP14.phx.gbl...I am creating a set of base classes and sub classes to use throughout aprogram 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 ageneric "dataobject" class to represent the table and the rows within thattable as a collection of objects with generic properties for manipulatingfields, 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 tooverride the Item property in the derived class and implement it to returnthe type you wanted to return. You cant return a speficic type (or morespecifically, cant return several different specific types) from a singlebase class."Rick Mogstad" <Ri**********@GMail.NOSPAM.com> wrote in messagenews: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 aprogram 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 ageneric "dataobject" class to represent the table and the rows within thattable as a collection of objects with generic properties for manipulatingfields, 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 这篇关于继承基类 - 无效的强制转换异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-14 08:42