By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,264 Members | 1,743 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,264 IT Pros & Developers. It's quick & easy.

is there way to convert array of objects to array of strings ?

P: n/a
i have array of objects that's are strings.
i need to convert it to array of string before sending to method.

is there a way to convert ? (short way..)
Nov 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
JV
You didn't really give enough detail to get a definite answer, but if it is
an actual array of strings, just typecast it.

For example (C#):

public void SomeFunctionOrOther (object[] actuallyAnArrayOfStrings)
{
string[] stringArray = (string[]) actuallyAnArrayOfStrings;
// followed by code that does something with it.
}
If you use VB you could try the CType() function

"roni" <xs*******@hotmail.com> wrote in message
news:d4**********@news2.netvision.net.il...
i have array of objects that's are strings.
i need to convert it to array of string before sending to method.

is there a way to convert ? (short way..)

Nov 22 '05 #2

P: n/a
No. Not a short way. You'd have to create a string array, iterate
through the object array, and populate your new string with
myojbect[i].ToString()

--
2005 Microsoft MVP C#
Robbe Morris
http://www.robbemorris.com
http://www.masterado.net/home/listings.aspx

"roni" <xs*******@hotmail.com> wrote in message
news:d4**********@news2.netvision.net.il...
i have array of objects that's are strings.
i need to convert it to array of string before sending to method.

is there a way to convert ? (short way..)

Nov 22 '05 #3

P: n/a
Roni,

\\\C#
object[] a = {"hello", "how", "are", "you"};
string[] b = new string[a.Length];
a.CopyTo(b, 0);
///

\\\VBNet
Dim a() As Object = {"hello", "how", "are", "you"}
Dim b(a.Length - 1) As String
a.CopyTo(b, 0)
///

I hope this helps,

Cor
Nov 22 '05 #4

P: n/a

object[] myobjarray = new object[5];

for(int i=0;i<5;i++)

{

myobjarray[i] = (object)i.ToString();

}

string[] mystrarray = (string[])myobjarray;

for(int i=0;i<5;i++)

{

Console.WriteLine(mystrarray[i]);

}

Console.ReadLine();
--
2005 Microsoft MVP C#
Robbe Morris
http://www.robbemorris.com
http://www.masterado.net/home/listings.aspx

"JV" <jo**********@goisc.com> wrote in message
news:OC*************@TK2MSFTNGP10.phx.gbl...
You didn't really give enough detail to get a definite answer, but if it
is an actual array of strings, just typecast it.

For example (C#):

public void SomeFunctionOrOther (object[] actuallyAnArrayOfStrings)
{
string[] stringArray = (string[]) actuallyAnArrayOfStrings;
// followed by code that does something with it.
}
If you use VB you could try the CType() function

"roni" <xs*******@hotmail.com> wrote in message
news:d4**********@news2.netvision.net.il...
i have array of objects that's are strings.
i need to convert it to array of string before sending to method.

is there a way to convert ? (short way..)


Nov 22 '05 #5

P: n/a
Robbe Morris [C# MVP] <in**@turnkeytools.com> wrote:
object[] myobjarray = new object[5];
for(int i=0;i<5;i++)
{
myobjarray[i] = (object)i.ToString();
}

string[] mystrarray = (string[])myobjarray;
for(int i=0;i<5;i++)
{
Console.WriteLine(mystrarray[i]);
}

Console.ReadLine();


Yes, there you've got an object array which happens to contain strings,
rather than a string array. You can't cast an object array to a string
array, despite all the contained objects being strings. (Just think
what would happen if you set myobjarray[i] = new object() afterwards.)

You can use Array.Copy though:

using System;

class Test
{
static void Main()
{
object[] myobjarray = new object[5];
for(int i=0;i<5;i++)
{
myobjarray[i] = (object)i.ToString();
}

string[] mystrarray = new string[myobjarray.Length];
Array.Copy(myobjarray, mystrarray, myobjarray.Length);

foreach (string x in mystrarray)
{
Console.WriteLine (x);
}
}
}
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #6

P: n/a
Robbe Morris [C# MVP] <in**@turnkeytools.com> wrote:
object[] myobjarray = new object[5];
for(int i=0;i<5;i++)
{
myobjarray[i] = (object)i.ToString();
}

string[] mystrarray = (string[])myobjarray;
for(int i=0;i<5;i++)
{
Console.WriteLine(mystrarray[i]);
}

Console.ReadLine();


Yes, there you've got an object array which happens to contain strings,
rather than a string array. You can't cast an object array to a string
array, despite all the contained objects being strings. (Just think
what would happen if you set myobjarray[i] = new object() afterwards.)

You can use Array.Copy though:

using System;

class Test
{
static void Main()
{
object[] myobjarray = new object[5];
for(int i=0;i<5;i++)
{
myobjarray[i] = (object)i.ToString();
}

string[] mystrarray = new string[myobjarray.Length];
Array.Copy(myobjarray, mystrarray, myobjarray.Length);

foreach (string x in mystrarray)
{
Console.WriteLine (x);
}
}
}
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #7

P: n/a
I was replying because the post above mine claimed something
I thought wouldn't work would.

--
2005 Microsoft MVP C#
Robbe Morris
http://www.robbemorris.com
http://www.masterado.net/home/listings.aspx

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Robbe Morris [C# MVP] <in**@turnkeytools.com> wrote:
object[] myobjarray = new object[5];
for(int i=0;i<5;i++)
{
myobjarray[i] = (object)i.ToString();
}

string[] mystrarray = (string[])myobjarray;
for(int i=0;i<5;i++)
{
Console.WriteLine(mystrarray[i]);
}

Console.ReadLine();


Yes, there you've got an object array which happens to contain strings,
rather than a string array. You can't cast an object array to a string
array, despite all the contained objects being strings. (Just think
what would happen if you set myobjarray[i] = new object() afterwards.)

You can use Array.Copy though:

using System;

class Test
{
static void Main()
{
object[] myobjarray = new object[5];
for(int i=0;i<5;i++)
{
myobjarray[i] = (object)i.ToString();
}

string[] mystrarray = new string[myobjarray.Length];
Array.Copy(myobjarray, mystrarray, myobjarray.Length);

foreach (string x in mystrarray)
{
Console.WriteLine (x);
}
}
}
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 22 '05 #8

P: n/a
I was replying because the post above mine claimed something
I thought wouldn't work would.

--
2005 Microsoft MVP C#
Robbe Morris
http://www.robbemorris.com
http://www.masterado.net/home/listings.aspx

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Robbe Morris [C# MVP] <in**@turnkeytools.com> wrote:
object[] myobjarray = new object[5];
for(int i=0;i<5;i++)
{
myobjarray[i] = (object)i.ToString();
}

string[] mystrarray = (string[])myobjarray;
for(int i=0;i<5;i++)
{
Console.WriteLine(mystrarray[i]);
}

Console.ReadLine();


Yes, there you've got an object array which happens to contain strings,
rather than a string array. You can't cast an object array to a string
array, despite all the contained objects being strings. (Just think
what would happen if you set myobjarray[i] = new object() afterwards.)

You can use Array.Copy though:

using System;

class Test
{
static void Main()
{
object[] myobjarray = new object[5];
for(int i=0;i<5;i++)
{
myobjarray[i] = (object)i.ToString();
}

string[] mystrarray = new string[myobjarray.Length];
Array.Copy(myobjarray, mystrarray, myobjarray.Length);

foreach (string x in mystrarray)
{
Console.WriteLine (x);
}
}
}
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.