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

R

roni

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..)
 
J

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
 
R

Robbe Morris [C# MVP]

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.ToString()
 
C

Cor Ligthert

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
 
R

Robbe Morris [C# MVP]

object[] myobjarray = new object[5];

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

{

myobjarray = (object)i.ToString();

}

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

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

{

Console.WriteLine(mystrarray);

}

Console.ReadLine();


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



JV said:
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 said:
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..)
 
J

Jon Skeet [C# MVP]

Robbe Morris said:
object[] myobjarray = new object[5];
for(int i=0;i<5;i++)
{
myobjarray = (object)i.ToString();
}

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

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 = 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 = (object)i.ToString();
}

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

foreach (string x in mystrarray)
{
Console.WriteLine (x);
}
}
}
 
R

Robbe Morris [C# MVP]

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 said:
Robbe Morris said:
object[] myobjarray = new object[5];
for(int i=0;i<5;i++)
{
myobjarray = (object)i.ToString();
}

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

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 = 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 = (object)i.ToString();
}

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

foreach (string x in mystrarray)
{
Console.WriteLine (x);
}
}
}
 

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

Top