stack to array

  • Thread starter Thread starter benben
  • Start date Start date
B

benben

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
 
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 said:
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.
 
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 said:
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 said:
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.
 
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 said:
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();
 
Back
Top