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 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
"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();
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.
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.
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
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
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();
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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#...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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: 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...
|
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: 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,...
| |