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..)
JV - 28 Apr 2005 14:54 GMT
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
>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..)
Robbe Morris [C# MVP] - 29 Apr 2005 02:52 GMT
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();

Signature
2005 Microsoft MVP C#
Robbe Morris
http://www.robbemorris.com
http://www.masterado.net/home/listings.aspx
> You didn't really give enough detail to get a definite answer, but if it
> is an actual array of strings, just typecast it.
[quoted text clipped - 13 lines]
>>
>> is there a way to convert ? (short way..)
Jon Skeet [C# MVP] - 29 Apr 2005 06:54 GMT
> object[] myobjarray = new object[5];
> for(int i=0;i<5;i++)
[quoted text clipped - 9 lines]
>
> 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);
}
}
}

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Robbe Morris [C# MVP] - 29 Apr 2005 14:23 GMT
I was replying because the post above mine claimed something
I thought wouldn't work would.

Signature
2005 Microsoft MVP C#
Robbe Morris
http://www.robbemorris.com
http://www.masterado.net/home/listings.aspx
>> object[] myobjarray = new object[5];
>> for(int i=0;i<5;i++)
[quoted text clipped - 38 lines]
> }
> }
Robbe Morris [C# MVP] - 28 Apr 2005 14:57 GMT
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()

Signature
2005 Microsoft MVP C#
Robbe Morris
http://www.robbemorris.com
http://www.masterado.net/home/listings.aspx
>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..)
Cor Ligthert - 28 Apr 2005 19:28 GMT
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