Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / General / April 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
roni - 28 Apr 2005 13:10 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.