473,396 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Collections, bases classes and inheritance


Hello,

I've got a c++ background and i'm wondering what's the best approach to this problem.

I've got a base class Animal with a set of minimal properties for an animal. From animal i've
derived a Dog and a Cat class which are extended with specific properties of it's species. Now i want
a collection to store these animals in. I've created an Collection derived from CollectionBase (see code).

But when i add a Dog or a Cat to this collection i get an exception even when i cast the cat or dog back
to it's base class.

Is there a collection to solve this problem. Which can take the base class

using System;
using System.Collections;

namespace Petshop
{
public class AnimalCollection : CollectionBase
{
public Animal this[ int index ]
{
get { return( (Animal) List[index] );}
set { List[index] = value;}
}

public int Add( Animal value )
{
return( List.Add( value ) );
}

public int IndexOf( Animal value )
{
return( List.IndexOf( value ) );
}

public void Insert( int index, Animal value )
{
List.Insert( index, value );
}

public void Remove( Animal value )
{
List.Remove( value );
}

public bool Contains( Animal value )
{
// If value is not of type Animal, this will return false.
return( List.Contains( value ) );
}

protected override void OnInsert( int index, Object value )
{
if (value.GetType() != Type.GetType("Petshop.Animal"))
{
throw new ArgumentException( "value must be of type Animal.", "value" );
}
}

protected override void OnRemove( int index, Object value )
{
if (value.GetType() != Type.GetType("Petshop.Animal"))
{
throw new ArgumentException( "value must be of type Animal.", "value" );
}
}

protected override void OnSet( int index, Object oldValue, Object newValue )
{
if (newValue.GetType() != Type.GetType("Petshop.Animal"))
{
throw new ArgumentException( "newValue must be of type Animal.", "newValue" );
}
}

protected override void OnValidate( Object value )
{
if (value.GetType() != Type.GetType("Petshop.Animal"))
{
throw new ArgumentException( "value must be of type Animal." );
}
}
}
}

Cheers

Erik

ps. No cats and dogs were harmed during testing of this code.

Nov 16 '05 #1
2 1159
Erik,
if (value.GetType() != Type.GetType("Petshop.Animal"))
{
throw new ArgumentException( "value must be of type Animal.", "value" );
}


Replaceing that with

if ( !(value is Petshop.Animal) ) ...

should get rid of the exceptions.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
No that won't work.
Did you try it?

The passed value is still a cat or a dog even after i cast the object to the base class in the Add methode.
Of course, casting from one reference type to another doesn't change
the actual object's type, only the type of reference you have to it
and which part of its interface you see.

so i still get the exception.


Where exactly (on which line) do you get the exception?

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #3

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

Similar topics

8
by: Dave | last post by:
Hello all, Suppose that derived inherits privately from base. A base pointer may not be made to point at a derived object in this case. I understand that is exactly what is supposed to happen...
62
by: christopher diggins | last post by:
Since nobody responded to my earlier post , I thought I would try to explain what I am doing a bit differently. When multiply inheriting pure virtual (abstract) base classes, a class obviously...
2
by: Howard Swope | last post by:
Could someone help explain thread safety issues in the System.Collections classes? The documentation states:...
22
by: Adam Clauss | last post by:
OK, I have class A defined as follows: class A { A(Queue<B> queue) { ... } } Now, I then have a subclass of both classes A and B. The subclass of A (SubA), more specifically is passed a...
5
by: Simon | last post by:
Hi all, I am writing a windows application using vb.net on the 1.1 framework. We have in the application, some strongly typed collections that have been written as classes that do not inherit...
8
by: digitalorganics | last post by:
What are the reason one would get this error: TypeError: Cannot create a consistent method resolution order (MRO) for bases object ?? I can provide the code if needed....
25
by: Lars | last post by:
Hi, I have a base class holding a generic list that needs to be accessed by both the base class and its subclasses. What is the best solution to this? I am fairly new to generics, but I am...
2
by: Wilson | last post by:
Hi, a very simple question. I am trying to understand inheritance using c++ and dont cee how i could use three classes to create an accounting program using inheritance. e.g one class containing...
47
by: Larry Smith | last post by:
I just read a blurb in MSDN under the C++ "ref" keyword which states that: "Under the CLR object model, only public single inheritance is supported". Does this mean that no .NET class can ever...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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...

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.