473,383 Members | 1,742 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,383 software developers and data experts.

Casting object[] -> string[]

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?
Nov 16 '05 #1
5 1883
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)


"AC" <sp**@aNOSPAMMEconnell.com> wrote in message
news:Ou**************@tk2msftngp13.phx.gbl...
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?
Nov 16 '05 #2
AC
Thanks for the reply... I am looking for an explicit cast option. This is
one option (brute force) I was hoping to avoid.

-AC

"Shakir Hussain" <sh**@fakedomain.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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)


"AC" <sp**@aNOSPAMMEconnell.com> wrote in message
news:Ou**************@tk2msftngp13.phx.gbl...
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?

Nov 16 '05 #3
AC <sp**@aNOSPAMMEconnell.com> wrote:
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));

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
AC
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 [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
AC <sp**@aNOSPAMMEconnell.com> wrote:
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));

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #5
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" <sp**@aNOSPAMMEconnell.com> wrote in message
news:eu**************@tk2msftngp13.phx.gbl...
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 [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
AC <sp**@aNOSPAMMEconnell.com> wrote:
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));

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 16 '05 #6

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

Similar topics

5
by: Vinodh Kumar | last post by:
I see that casting changes the value of a pointer in case of multiple inheritance.In single inheritance also it is the same know?Isn't it? Vinodh Kumar P
4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
18
by: Marco | last post by:
I need to get a iterator from any generic collection. public class .... GetIterator(Object collection) { ..... }
61
by: Ken Allen | last post by:
I am relatively new to .Net, but have been using VB and C/C++ for years. One of the drawbacks with VB6 and earlier was the difficulty in casting a 'record' to a different 'shape' so one could...
23
by: René Nordby | last post by:
Hi there, Is there anyone that knows how to do the following? I have a class A and a class B, that 100% inherits from class A (this means that I don't have other code in class B, than...
7
by: William S Fulton | last post by:
I'm looking for the name of the following casting style in order to do some reading around on it and why it is sometimes used. unsigned long long ull = 0; void * ptr = 0; ull = *(unsigned...
1
by: madumm | last post by:
Hi all I really need a solution for the following:: ------------------------------------------------------------------------------------- Say i have a font object called 'myFont' as shown below;...
9
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
19
by: =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?= | last post by:
I'm doing my c# more and more like i used to code c++, meaning i'm casting more often than creating an instance of objects. like : protected void gvOrderDetailsRowDataBound(object sender,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.