473,666 Members | 2,571 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)resultS tack.ToArray();

I've tried casting it as a string, an array of strings... no luck. ideas?
Nov 16 '05 #1
5 1892
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().ToC harArray());
}
--
Shak
(Houston)


"AC" <sp**@aNOSPAMME connell.com> wrote in message
news:Ou******** ******@tk2msftn gp13.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)resultS tack.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**@fakedomai n.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.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().ToC harArray());
}
--
Shak
(Houston)


"AC" <sp**@aNOSPAMME connell.com> wrote in message
news:Ou******** ******@tk2msftn gp13.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)resultS tack.ToArray();

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

Nov 16 '05 #3
AC <sp**@aNOSPAMME connell.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)resultS tack.ToArray();

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


string[] results = (string[])resultStack.To Array(typeof(st ring));

--
Jon Skeet - <sk***@pobox.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
AC <sp**@aNOSPAMME connell.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)resultS tack.ToArray();

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


string[] results = (string[])resultStack.To Array(typeof(st ring));

--
Jon Skeet - <sk***@pobox.co m>
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)objArra y[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**@aNOSPAMME connell.com> wrote in message
news:eu******** ******@tk2msftn gp13.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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
AC <sp**@aNOSPAMME connell.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)resultS tack.ToArray();

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


string[] results = (string[])resultStack.To Array(typeof(st ring));

--
Jon Skeet - <sk***@pobox.co m>
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
2278
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
3459
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 good way to do this is by example. So I will give an example and please tell me what you think: I have a base class A with a virtual destructor, and a class B that is it inherits publicly from A and defines som extra stuff.
7
3664
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; but it complains for the following lines
18
2164
by: Marco | last post by:
I need to get a iterator from any generic collection. public class .... GetIterator(Object collection) { ..... }
61
4550
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 perform different manipulations on it. For example, I have a complex data structure, which I can represent in a VB6 TYPE declaration, but I cannot easily convert that to a fixed length array of unsigned bytes so that I could perform a checksum...
23
3505
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 the Inherit statement).
7
2643
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 long long*)&ptr; As opposed to the more usual casting:
1
1210
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; Font myFont= domainAxis.getTickLabelFont(); I need to cast "myFont" object into a "Element" object as i shown below
9
3458
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
1941
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, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { switch (((Sale)e.Row.DataItem).SzPN) {
0
8454
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8363
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8883
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8787
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7389
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4200
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2776
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.