473,769 Members | 5,900 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inheriting a base class - Invalid Cast Exception

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 "Manufactur ers" object that inherits from
DataCollection and a "Manufactur er" 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 objDataCollecti on

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

Inherits objDataCollecti on

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

Get

return MyBase.Item(cod e)

End Get

End Property

End Class

Public Class objManufacturer

Inherits objDataObject

..

End Class


Nov 21 '05
10 2782
I'm back to my inheritance problem again, with a new twist.

My DataCollection object exposes a readonly "collection " property that
returns a collection of DataObject objects.

I want my Manufacturers object (a subclass of my DataCollection object)
to return a collection of Manufacturer object, but since the code for
creating the collection is in my base class, it doesn't know what type
of objects the subclass will be wanting to the code creates a collection
of DataObject objects... and then when I try to loop through them as
Manufacturers to get the extra functionality of the Manufacturer object,
I cannot.

Here is my current code to create the collection. This is part of the
base class DataCollection:

Public Class DataCollection
Public Readonly Property Collection() as ICollection
Get
'Gets an array of rows to build the collection from
Dim Rows() as DataRow = mDataTable.Sele ct()
Dim Row as DataRow
Dim Col as New Collection
For Each Row in Rows
'Create an object, the constructor utilizes the current row
Dim obj as New DataObject(row)
Col.Add(obj,obj .Key)
Next Row
Return Col
End Get
End Property
End Class

Here then is the code I want to use to loop through each manufacturer

Dim Mans as new Manufacturers
Dim Man as Manufacturer
For each Man in Mans.Collection
Msgbox Man.Manufacture rName
Next Man

The problem is that Mans.Collection returns a collection of DataObject
objects, not Manufacturer objects. I cannot figure out how to tell the
Collection property of the DataCollection object to create a collection
of differently types objects depending on what is inheriting it, because
of course it doesn't know. The only thing I can think of doing is
creating an Overriding property in the Manufacturers object like this:

Public Overrides Readonly Property Collection() as ICollection
Get
Dim ColNew as New Collection
Dim DO as DataObject
For Each DO in MyBase.Collecti on
ColNew.Add DO.Key,New Manufacturer(do )
Next DO
Return ColNew
End Get
End Property

This code loops through all the DataObjects in the collection and
transforms them into Manufacturer objects using the code we talked about
earlier where I pass the DataObject to the constructor of my
Manufacturer object.

Are there any better ways around this? One that WON'T have me writing a
block of code every time I need to create a collection property, which
is part of the reason I'm trying to create a base class for.

Nov 21 '05 #11

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

Similar topics

7
1423
by: Ray Gardener | last post by:
C++ doesn't allow: class counter : public int {}; Was there any special reason why base class ids can only be struct/class ids? This limitation appears to violate the concept of uniform type conceptualization; i.e. classes can never have or extend an is-a relationship to a scalar.
11
2168
by: Noah Coad [MVP .NET/C#] | last post by:
How do you make a member of a class mandatory to override with a _new_ definition? For example, when inheriting from System.Collections.CollectionBase, you are required to implement certain methods, such as public void Add(MyClass c). How can I enforce the same behavior (of requiring to implement a member with a new return type in an inherited class) in the master class (similar to the CollectionBase)? I have a class called...
3
2526
by: sureshsundar007 | last post by:
Hi all, I'm trying to inherit the SimpleWorkerRequest class but cant able to do so. I'm getting an error called "System.Web.Hosting.SimpleWorkerRequest.SimpleWorkerRequest()' is inaccessible due to its protection level" I tried my dervived class with public,internal and private
3
5463
by: Rob Richardson | last post by:
Greetings! I am trying to make it possible for new derived classes of an object to be used by my application without rebuilding the application. The new classes will be made known to my application by storing the file name, assembly name and class name for them in a database. The code to create an instance of the object will look something like the following: Assembly asm = Assembly.LoadFrom(derivedFileName); Type derivedType =...
5
15237
by: Chris Capon | last post by:
Is there any way to cast a base class object to a derived class datatype when the derived class adds no new fields nor alters the object allocation in any way? The scenario would be where you want to extend a base class by adding only methods and properties, however, only base class objects are provided to you. Here is a sample: public class Base
10
1748
by: Brett Romero | last post by:
Say I have a class inheriting some base class: BaseClass { void Foo() { Update(); } }
0
2146
by: robert | last post by:
Hi all, I'm having a hard time resolving a namespace issue in my wsdl. Here's an element that explains my question, with the full wsdl below: <definitions name="MaragatoService" targetNamespace="http://swaMaragatoNS" xmlns:tns="http://swaMaragatoNS" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
24
39161
by: AtariPete | last post by:
Hey All, I have a C# question for you regarding up casting (base to derived). I was wondering about the most elegant way (readable, less code) to cast from a base type to its derived type. Please consider the following two classes: class Base { private int m_valA;
12
2168
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, I have a class which "BiologySequence" which looks about like this. public class BiologySequence { private string _Sequence; public string Sequence {
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10210
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9990
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9861
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5298
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3956
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2814
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.