Casting object[] -> string[]

  • Thread starter Thread starter AC
  • Start date Start date
A

AC

I've put some strings into a stack, and now I want to convert the stack to an string based array. Here's what I'm trying to do (this returns a cast error)

string[] results = (string)resultStack.ToArray();

I've tried casting it as a string, an array of strings... no luck. ideas?
 
AC,

There are different ways to do this, One of them may be.

//define new string array with the size of object array
string [] strArray= new string [objArray.Length];

//loop it out and fill the values
for(int j=0;j<strArray.Length;j++)
{
strArray[j] = new string(objArray[j].ToString().ToCharArray());
}


--
Shak
(Houston)




I've put some strings into a stack, and now I want to convert the stack to
an string based array. Here's what I'm trying to do (this returns a cast
error)

string[] results = (string)resultStack.ToArray();

I've tried casting it as a string, an array of strings... no luck. ideas?
 
Thanks for the reply... I am looking for an explicit cast option. This is
one option (brute force) I was hoping to avoid.

-AC
 
AC said:
I've put some strings into a stack, and now I want to convert the
stack to an string based array. Here's what I'm trying to do (this
returns a cast error)

string[] results = (string)resultStack.ToArray();

I've tried casting it as a string, an array of strings... no luck.
ideas?

string[] results = (string[])resultStack.ToArray(typeof(string));
 
Makes sense when yo usee the answer. Thanks John. Wasn't aware .ToArray()
took an optional parm on what to case the array as.

-AC

Jon Skeet said:
AC said:
I've put some strings into a stack, and now I want to convert the
stack to an string based array. Here's what I'm trying to do (this
returns a cast error)

string[] results = (string)resultStack.ToArray();

I've tried casting it as a string, an array of strings... no luck.
ideas?

string[] results = (string[])resultStack.ToArray(typeof(string));
 
Not a cast, really: the parameter tells ToArray what sort of array to
create. Shakir's solution

//define new string array with the size of object array
string [] strArray= new string [objArray.Length];

//loop it out and fill the values
for(int j=0;j<strArray.Length;j++)
{
strArray[j] = (string)objArray[j];
}

is more or less what ToArray() does internally, with the expected result if
some member of the array isn't a string (i.e. an exception).

AC said:
Makes sense when yo usee the answer. Thanks John. Wasn't aware ..ToArray()
took an optional parm on what to cast the array as.

-AC

Jon Skeet said:
AC said:
I've put some strings into a stack, and now I want to convert the
stack to an string based array. Here's what I'm trying to do (this
returns a cast error)

string[] results = (string)resultStack.ToArray();

I've tried casting it as a string, an array of strings... no luck.
ideas?

string[] results = (string[])resultStack.ToArray(typeof(string));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top