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

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

am 28.04.2005 15:05:27 von tony owen

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

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

am 28.04.2005 15:54:14 von 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" wrote in message
news:d4qj3m$16v$1@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..)
>
>

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

am 28.04.2005 15:57:39 von Robbe Morris

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" wrote in message
news:d4qj3m$16v$1@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..)
>
>

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

am 28.04.2005 20:28:38 von 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

I get a specified cast invalid

am 29.04.2005 03:52:56 von Robbe Morris

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" wrote in message
news:OCIiEn$SFHA.140@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" wrote in message
> news:d4qj3m$16v$1@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..)
>>
>>
>
>

Re: I get a specified cast invalid

am 29.04.2005 07:54:14 von skeet

Robbe Morris [C# MVP] 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 -
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Right, I was replying to what appeared to be faulty code

am 29.04.2005 15:23:04 von Robbe Morris

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]" wrote in message
news:MPG.1cdbdc63fa55a6ad98c063@msnews.microsoft.com...
> Robbe Morris [C# MVP] 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 -
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too