471,306 Members | 1,380 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

object projection

Hi,

this is my problem:
-------------------
public class Rectangle : Shape
{}
....
Shape shape;
shape = graph.AddShape(uid, pointXY);
----------------------------
AddShape returns a Shape object but in fact that is a Rectangle object, how
do I project it to this class in order to use Rectangles methods and
variables?

I got stuck; thanks for help
Adam Wawrzyniak
Jan 18 '06 #1
5 2087
Adam Wawrzyniak wrote:
this is my problem:
-------------------
public class Rectangle : Shape
{}
...
Shape shape;
shape = graph.AddShape(uid, pointXY);
----------------------------
AddShape returns a Shape object but in fact that is a Rectangle object, how
do I project it to this class in order to use Rectangles methods and
variables?


You need to cast:

Shape shape = (Rectangle) graph.AddShape(uid, pointXY);

Jon

Jan 18 '06 #2
I think that should be:

Rectangle rect = (Rectangle) graph.AddShape(uid, pointXY);

Note also that you should be pretty sure that you are expecting a Rectangle;
if not, you should look at the "is" and "as" operators on MSDN2.

Marc
Jan 18 '06 #3
Thanks
Adam

"Marc Gravell" <mg******@rm.com> schrieb im Newsbeitrag
news:eL**************@TK2MSFTNGP15.phx.gbl...
I think that should be:

Rectangle rect = (Rectangle) graph.AddShape(uid, pointXY);

Note also that you should be pretty sure that you are expecting a
Rectangle; if not, you should look at the "is" and "as" operators on
MSDN2.

Marc

Jan 18 '06 #4
Marc Gravell wrote:
I think that should be:

Rectangle rect = (Rectangle) graph.AddShape(uid, pointXY);


Sorry, yes. It's that old "shouldn't post before first cup of coffee"
problem. I'd love to say "It won't happen again" but that would be
overly optimistic...

Jon

Jan 18 '06 #5
<Laughs/> Very familiar with that problem; have you *seen* the amount of
times I post a chain all on my own correcting some typo!

Plus - its so rare that I get to find fault in your code that I couldn't
resist ;-p

Marc

Jan 18 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Ajith Prasad | last post: by
1 post views Thread by Bijay Kumar | last post: by
1 post views Thread by anirbanab | last post: by
reply views Thread by Bijay Kumar | last post: by
reply views Thread by freshman | last post: by
reply views Thread by Garmt de Vries-Uiterweerd | last post: by
3 posts views Thread by dariosalvi78 | last post: by
4 posts views Thread by xiao | last post: by
reply views Thread by rosydwin | last post: by

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.