Casting derived

S

Steven Wolf

Hi folks,

i developing right now a big project for my company and i have follow
issue:

I have a base class for all my domain objects (abstract DomainObject)
and many objects derived from that class, like Facility:DomainObject.

I have also a collection class (DomainObjectCollection:CollectionBase)
which can add [public void Add (DomainObject domainObject)] and return
[public DomainObject this [int index]] only DomainObject's.

When i try to get a domain object from the collection, all the time i
have to play with casting, like:

....
Facility facility;
facility = (Facility)domainObjectCollection[3]; // Return domain
object
....

but i can do..

domainObjectCollection.Add(facility)

...without any conversion/casting problems, but i MUST cast when
retrieve, why?
isnt there any cool solution? it looks pretty ugly all the time when i
have to cast.. :-(

thanks!
Steven.
 
J

Jeff Louie

Steven... You can modify this code.

// provide an indexer
public Drawable this[int index]
{
get
{
// throws ArgumentOutOfRangeException
return (Drawable)List[index];
}
set
{
//throws ArgumentOutOfRangeException
Insert(index,value);
}
}


You can then use the indexer like this:

// create a DrawableCollection
DrawableCollection dc= new DrawableCollection();
dc.Add(new Circle());
// test indexer
Drawable draw= dc[0];

Regards,
Jeff
 
S

Steven Wolf

Hi Jeff,

thats exactly what i'm using, only the last line in your posted code
is different:
// create a DrawableCollection
DrawableCollection dc= new DrawableCollection();
dc.Add(new Circle());
// test indexer
Drawable draw= dc[0];


i use:

Circle myCircle = dc[0];

and that doesnt work (no implicit conversions between...)
but Circle is derived from Draw, and the collection return only Draw
objects, so i must use:

Circlce myCircle= (Circle)dc[0];

...which is pretty nerving if u have to use it all the time.



Steven.


Jeff Louie said:
Steven... You can modify this code.

// provide an indexer
public Drawable this[int index]
{
get
{
// throws ArgumentOutOfRangeException
return (Drawable)List[index];
}
set
{
//throws ArgumentOutOfRangeException
Insert(index,value);
}
}


You can then use the indexer like this:

// create a DrawableCollection
DrawableCollection dc= new DrawableCollection();
dc.Add(new Circle());
// test indexer
Drawable draw= dc[0];

Regards,
Jeff
 
J

Jeff Louie

Steven... I now understand what you are trying to do :). In most
instances I
just use the polymorphic behaviour of the base class drawable.Draw()
rather
than circle.Draw() so I don't need to cast. As you know there is no
guarantee
that dc[0] is a Circle so the cast is very much appropriate since it
could result
in an exception being thrown.

If I know that a private collection will only contain Circles, I
sometimes write
getters that return the specific type or even an array eg Circles[].
The caller is
not aware that I am using a null safe base type safe collection
internally. The
caller only sees the AddCircle, ClearCircles and GetCircles() methods.

Regards,
Jeff
but Circle is derived from Draw, and the collection return only Draw
objects, so i must use:

Circlce myCircle= (Circle)dc[0];
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top