473,387 Members | 1,463 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,387 software developers and data experts.

Casting derived

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.
Nov 17 '05 #1
4 1267
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

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #2
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 <je********@yahoo.com> wrote in message news:<OE*************@TK2MSFTNGP15.phx.gbl>... 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

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #3
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];

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #4
Ok, i see.

thank you a lot Jeff,

Steven.
Jeff Louie <je********@yahoo.com> wrote in message news:<#5**************@tk2msftngp13.phx.gbl>...
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];

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #5

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

Similar topics

5
by: Vinodh Kumar | last post by:
I see that casting changes the value of a pointer in case of multiple inheritance.In single inheritance also it is the same know?Isn't it? Vinodh Kumar P
5
by: Suzanne Vogel | last post by:
** Isn't the 'static_cast' operator the same as traditional type casting? ie, Aren't the following ways of getting b1, b2 the same? // 'Derived' is derived from 'Base' Derived* d = new...
3
by: Kurt | last post by:
i just can't figure out why something im doing is not working correctly.... public interface IInterface { int someProperty { get; set; }
0
by: Kurt Lange | last post by:
no... the array is created dynamically. and no... that defeats the purpose of what im trying todo.. encapsulate all initializing of variables in base class... derive from it... by deriving...
7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
24
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....
9
by: Jess | last post by:
Hello, It seems both static_cast and dynamic_cast can cast a base class pointer/reference to a derived class pointer/reference. If so, is there any difference between them? In addition, if I...
9
by: Naomi | last post by:
I need to make software engineering decision to do with using a derived data type in a container class. So for example, if I have an Edge class, and I want to make a Edge object which contains two...
9
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
12
by: Taras_96 | last post by:
Hi everyone, Herb Schildt in "C++, the complete reference" writes: "In C++, it is illegal to convert one type of pointer into another without the use of an explicit type cast. For this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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...
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...

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.