473,396 Members | 1,683 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.

stack to array

How can I convert a stack to array?

I have a Stack of System.Drawing.Point's and I would like to invoke
Graphics.DrawCurve, which takes a Point[] array as parameter.

I tried Stack.ToArray but it only returns an object[].

I tried to do element copy, but I cannot foresee the size of the stack (so I
cannot allocate the array anyway.)

Any suggestion would be greatly appreciated!

ben

Nov 16 '05 #1
7 3474
how about something like this....

System.Collections.Stack stack = new System.Collections.Stack();
Point pt1 = new Point(10,10);
Point pt2 = new Point(20,20);
Point pt3 = new Point(20,30);
stack.Push(pt1);
stack.Push(pt2);
stack.Push(pt3);
Point[] pts = new Point[stack.Count];
stack.ToArray().CopyTo(pts, 0);

HTH

Ollie Riches

"benben" <be******@yahoo.com.au> wrote in message
news:eq**************@TK2MSFTNGP09.phx.gbl...
How can I convert a stack to array?

I have a Stack of System.Drawing.Point's and I would like to invoke
Graphics.DrawCurve, which takes a Point[] array as parameter.

I tried Stack.ToArray but it only returns an object[].

I tried to do element copy, but I cannot foresee the size of the stack (so I cannot allocate the array anyway.)

Any suggestion would be greatly appreciated!

ben

Nov 16 '05 #2
"benben" <be******@yahoo.com.au> wrote in message
news:eq**************@TK2MSFTNGP09.phx.gbl...
How can I convert a stack to array?

I have a Stack of System.Drawing.Point's and I would like to invoke
Graphics.DrawCurve, which takes a Point[] array as parameter.

I tried Stack.ToArray but it only returns an object[].


Wouldn't this work?
Stack myStack = new Stack();
.... Add Points
Point[] points = (Point[]) myStack.ToArray();
Nov 16 '05 #3
I think that you're confusing compile-time type checking with run-time
typing of actual object instances.

Yes, Stack.ToArray() is declared as returning an array of Object, but
that doesn't mean that at run time each entry in the array will be
nothing more than an Object type. Remember that at run time the Stack
is a collection of instances, and each instance knows what type it is.

So, when you say Stack.ToArray(), you will get an array, and each entry
in the array will know what type it is.

Therefore, as Sean pointed out, you can say:

Point[] ptArray = (Point[])theStack.ToArray();

and this should work. In fact, the only reason that you need to do this
at all is to keep the compiler happy, as DrawCurve indicates that it
wants an array of points. If the compiler would only turn a blind eye,
you could just pass theStack.ToArray() directly to DrawCurve, which
would discover upon walking through the array that each instance in the
array was a boxed Point object.

Again: compile time is one thing. The compiler checks expected types
against declarations. Run time is another thing: every object instance
knows its type.

Nov 16 '05 #4
Thanks for your reply!

I eventually reached Ollie's method and it worked fine.

I did try the way you suggested but it didn't compile. Just because that a
Point is an Object (by boxing in this case), doesn't mean that a Point[] is
also an Object[]. Both Point[] and Object[] are types derived from
System.Array, and since only single inheritance is supported by the CLR, we
cannot say that a Point[] is really an Object[]. Frankly, I can cast each
element of the Object[] array before I use them, and this has always been
the way I deal with Object[]. But passing the array into a function is
another story, you see, the function when written doesn't cast each element
from the array before use, and the compiler doesn't know how to convert an
Object[] to a Point[], and hence a compiler error.
I think that you're confusing compile-time type checking with run-time
typing of actual object instances.

Yes, Stack.ToArray() is declared as returning an array of Object, but
that doesn't mean that at run time each entry in the array will be
nothing more than an Object type. Remember that at run time the Stack
is a collection of instances, and each instance knows what type it is.

So, when you say Stack.ToArray(), you will get an array, and each entry
in the array will know what type it is.

Therefore, as Sean pointed out, you can say:

Point[] ptArray = (Point[])theStack.ToArray();

and this should work. In fact, the only reason that you need to do this
at all is to keep the compiler happy, as DrawCurve indicates that it
wants an array of points. If the compiler would only turn a blind eye,
you could just pass theStack.ToArray() directly to DrawCurve, which
would discover upon walking through the array that each instance in the
array was a boxed Point object.

Again: compile time is one thing. The compiler checks expected types
against declarations. Run time is another thing: every object instance
knows its type.

Nov 16 '05 #5
Thanks Ollie! That's exactly what I figured out!!! I don't know why I didn't
find the Count property, probably too sleepy...

Anyway thanks a lot!

ben
how about something like this....

System.Collections.Stack stack = new System.Collections.Stack();
Point pt1 = new Point(10,10);
Point pt2 = new Point(20,20);
Point pt3 = new Point(20,30);
stack.Push(pt1);
stack.Push(pt2);
stack.Push(pt3);
Point[] pts = new Point[stack.Count];
stack.ToArray().CopyTo(pts, 0);

HTH

Ollie Riches

"benben" <be******@yahoo.com.au> wrote in message
news:eq**************@TK2MSFTNGP09.phx.gbl...
How can I convert a stack to array?

I have a Stack of System.Drawing.Point's and I would like to invoke
Graphics.DrawCurve, which takes a Point[] array as parameter.

I tried Stack.ToArray but it only returns an object[].

I tried to do element copy, but I cannot foresee the size of the stack
(so I
cannot allocate the array anyway.)

Any suggestion would be greatly appreciated!

ben


Nov 16 '05 #6
Bruce Wood <br*******@canada.com> wrote:
I think that you're confusing compile-time type checking with run-time
typing of actual object instances.

Yes, Stack.ToArray() is declared as returning an array of Object, but
that doesn't mean that at run time each entry in the array will be
nothing more than an Object type. Remember that at run time the Stack
is a collection of instances, and each instance knows what type it is.

So, when you say Stack.ToArray(), you will get an array, and each entry
in the array will know what type it is.

Therefore, as Sean pointed out, you can say:

Point[] ptArray = (Point[])theStack.ToArray();

and this should work.


No it won't, for two reasons:

1) Point is a value type - Point[] doesn't derive from Object[] because
of this, so you can't cast from Object[] to Point[]
2) Even if you had reference types instead (eg string), the array
returned by Stack.ToArray really *is* just an object[], even if
every element in it is a string. That's why ArrayList has
ToArray(Type) which returns an *actual* array of the specified
type. Unfortunately, Stack doesn't have such a method.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
No it doesn't, as it turned out.

The compiler prohibits me from casting an Object[] to a Point[].

Thanks for your reply anyway!

ben
"benben" <be******@yahoo.com.au> wrote in message
news:eq**************@TK2MSFTNGP09.phx.gbl...
How can I convert a stack to array?

I have a Stack of System.Drawing.Point's and I would like to invoke
Graphics.DrawCurve, which takes a Point[] array as parameter.

I tried Stack.ToArray but it only returns an object[].


Wouldn't this work?
Stack myStack = new Stack();
... Add Points
Point[] points = (Point[]) myStack.ToArray();

Nov 16 '05 #8

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

Similar topics

7
by: Aguilar, James | last post by:
Hello all, To begin, yes, this -is- a homework assignment. However, it is for my Algorithms class, and my instructor has given us explicit permission to use "expert" groups like newsgroups, so...
2
by: James | last post by:
Hi, I am using Visual C++ 6.0. I got a "stack overflow" error message when running the program because of a "double array". I have a computer with 1GB memory. Can I extend the memory for the...
4
by: Chris Mabee | last post by:
Hello all, and Merry Christmas, I'm having a problem understanding an example of an array based implementation of a stack in a textbook of mine. The code in question is written below. The syntax...
2
by: ronjon | last post by:
I am trying to corelate C code and its associated assembly code produced by gcc. In particular,I am trying to figure out how the stack pointer increments during variable initialization inside a...
5
by: Gomaw Beoyr | last post by:
Hello Is there any explanation why Microsoft chose to implement arrays as objects allocated on the heap instead of structs allocated on the stack? For "mathematical stuff", one normally...
1
by: alfie27 | last post by:
I currently have a working program that is a stack that stores integers. Now i have to convert it to store strings instead of integers. I have been working on this for hours and just keep getting...
9
by: coder_lol | last post by:
Thanks everybody for helping me with the Syntax confusion! The implicit conversion stuff really got me :) I have one more question... Array<int32ia; Does the above use the default...
9
by: Tarique | last post by:
Hello all.I am trying to implement a stack which can store either integer or float values. The code is given below: #include<stdio.h> #include<stdlib.h> #include<string.h> #define...
1
by: mattmao | last post by:
Hello everyone, this is my first thread in this .NET forum. Since I am studying C#.NET in this semester, I reckon this would be just the right place for my asking questions regarding the C#...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.