473,385 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
8 27817
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
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
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

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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Robert Oschler | last post by:
With PHP 4, is there a way to convert an associative array to an object? For example, suppose I have the following arrays inside a nested associative array ($nestedAA): $AA1 = 'fieldValue1'; ...
6
by: Ricardo Quintanilla | last post by:
i have a code that sends data to a socket listening over as400 platform, the socket responds to me as a "byte array". then i need to convert the "byte array" into a string. the problem is that...
15
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
6
by: Petar Popara | last post by:
I need some help converting byte into Array: int bufferLen = Convert.ToInt32(file.Length); byte buffer = new byte; int len = sr.Read(buffer, 0, bufferLen); ArrayList a = new ArrayList(len);...
10
by: Steve | last post by:
this code: private Array m_arrays = new Array; results in an array of 3 null Array objects. What am I missing?
5
by: Peter Nofelt | last post by:
Hi all, My scenario is as follows: * Receive a base 1 array (index starts at 1 instead of 0) of data from a COM component . * Need to pass this array to a .net component that requires its...
3
by: David | last post by:
Hi, how to convert a char array(byte) to a string variable? byte buffer; string strTest; /*the blow codes all get type of 'buffer': System.Byte! strTest = buffer.ToString() strTest =...
1
by: mangala | last post by:
i hav gotta convert an int array to a char array.. so that i can write it to the socket.. that is in the server side.. n in the client side i gotta read d socket... get d char array n convert it...
4
by: Bosconian | last post by:
I receive the following array via an XML feed: Array ( =Array ( =290** =1 2** =Vibratory Soil** =Vibratory Soil Compactors**
2
by: Frnk Carleo | last post by:
I am looking to convert a char array defined as char pstrInputClaim to a std::wstring trying to use the constructor as below std::wstring strInputClaim(pstrInputClaim);
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.