x


Downcasting Array to int (without #pragma downcast)

How can I cast a single element from an Array to an integer, while #pragma strict is on, and without using #pragma downcast?

#pragma strict
private var        _index : int = 1;
private var        possibleIndices : Array = new Array();
private var        currentIndex : int;

// the following only works when #pragma downcast is active
locationIndex = possibleLocations[_index];

// the following fails when #pragma downcast is not active
locationIndex = possibleLocations[_index] as int;
more ▼

asked May 31 '11 at 07:10 PM

Toxic Blob gravatar image

Toxic Blob
656 18 20 33

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

I don't think you can - there's no cast operator

I would honestly skip on using Array, it's untyped and will cause a fairly large amount of garbage when boxing/unboxing your ints

Instead, use a List., like so:

import System.Collections.Generic;
private var possibleIndices = new List.<int>();

You can then use .Add to add things in, and access items with possibleIndices[i] as before

more ▼

answered May 31 '11 at 07:20 PM

Mike 3 gravatar image

Mike 3
30.5k 10 65 253

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1363
x32

asked: May 31 '11 at 07:10 PM

Seen: 1130 times

Last Updated: May 31 '11 at 07:40 PM