473,406 Members | 2,698 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,406 software developers and data experts.

Easy way to cast an array of strings to an array of objects?

I need to cast a string[] into an object[] and vice versa.

At the moment, I'm using code like this:

int numOfObjects = Values.Length;
object[] objects = new object[numOfObjects];
for(int i = 0; i < numOfObjects; i++) {
objects[i] = (object)Values[i];
}

where Values is a string[] passed to the member doing the conversion.

It works but it seems clunky. I feel there should be an easier way.
Is there?

Nov 17 '05 #1
11 22743
use (object[])stringArray

string[] stringItems = "a,b,c,d,e".Split(',');
object[] objectItems = (object[])stringItems;

Nov 17 '05 #2
you can't cast string[] to object[], but what you can do is use the
Array.Copy function instead of doing your own loop.

"ssg31415926" wrote:
I need to cast a string[] into an object[] and vice versa.

At the moment, I'm using code like this:

int numOfObjects = Values.Length;
object[] objects = new object[numOfObjects];
for(int i = 0; i < numOfObjects; i++) {
objects[i] = (object)Values[i];
}

where Values is a string[] passed to the member doing the conversion.

It works but it seems clunky. I feel there should be an easier way.
Is there?

Nov 17 '05 #3
sorry, I take that back, you can actually cast string[] to object[].

"Daniel Jin" wrote:
you can't cast string[] to object[], but what you can do is use the
Array.Copy function instead of doing your own loop.

"ssg31415926" wrote:
I need to cast a string[] into an object[] and vice versa.

At the moment, I'm using code like this:

int numOfObjects = Values.Length;
object[] objects = new object[numOfObjects];
for(int i = 0; i < numOfObjects; i++) {
objects[i] = (object)Values[i];
}

where Values is a string[] passed to the member doing the conversion.

It works but it seems clunky. I feel there should be an easier way.
Is there?

Nov 17 '05 #4
Ah. Publicly embarassed with a simple solution. Serves me right for
being lazy!

I'd tried casting object[] to string[] and it didn't work so I assumed
it wouldn't work the other way and didn't test it. I've used the
simple cast to sort out string[] to object[] conversion as you just
suggested and Array.CopyTo to sort out the former problem:

object[] objects =
(object[])this.m_DirectoryEntry.Properties[PropertyName].Value;
string[] strings = new string[objects.Length];
objects.CopyTo(strings, 0);
return strings;

Thanks very much for your help, Daniel and gmiley.

Nov 17 '05 #5
You can use the same code to go either way, I just tested it to double
check and it does work.
object[] objects =
(object[])this.m_DirectoryEntry.Properties[PropertyName].Value;

string[] strings = (string[])objects;
return strings;

Nov 17 '05 #6
I soooo can't wait for Generics ;)

List<string> blahList = new
List<string>(this.m_DirectoryEntry.Properties[PropertyName].Value);

string[] strings = blahList.ToArray();

Bill Priess, MCP

"gmiley" <gm****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
You can use the same code to go either way, I just tested it to double
check and it does work.
object[] objects =
(object[])this.m_DirectoryEntry.Properties[PropertyName].Value;

string[] strings = (string[])objects;
return strings;

Nov 17 '05 #7
Works for you but doesn't work for me! Weird. This code:

object[] objects =
(object[])this.m_DirectoryEntry.Properties[PropertyName].Value;
string[] strings = (string[])objects;
return strings;

gives:

An unhandled exception of type 'System.InvalidCastException' occurred
in utopiaadhelper.dll

Additional information: Specified cast is not valid.

Nov 17 '05 #8
ssg31415926 <ne**********@gmail.com> wrote:
Works for you but doesn't work for me! Weird. This code:

object[] objects =
(object[])this.m_DirectoryEntry.Properties[PropertyName].Value;
string[] strings = (string[])objects;
return strings;

gives:

An unhandled exception of type 'System.InvalidCastException' occurred
in utopiaadhelper.dll

Additional information: Specified cast is not valid.


The difference is probably that your directory entry isn't *actually* a
string array, it's an object array which contains only strings.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #9
So, are you saying you can have an object array which is actually a
string array (containing strings) and an object array which is acually
an object array (containing strings)? How does that work?

Is this because DirectoryServices is using ADSI behind the scenes and
because the data is coming out of a COM object or is it possible to see
this using objects that have only ever been .NET objects?

SSG

Nov 17 '05 #10
ssg31415926 <ne**********@gmail.com> wrote:
So, are you saying you can have an object array which is actually a
string array (containing strings) and an object array which is acually
an object array (containing strings)? How does that work?
Any array knows what kind of objects it can contain. So you can do:

object[] foo = new object[1];
foo[0] = "hello";

and foo will be of type object[]. This is different to:

string[] bar = new string[1];
bar[0] = "hello";

bar is of type string[]. You could then do:

object[] baz = bar;

and although the variable baz is of type object[], the actual value is
a reference to a string[].
Is this because DirectoryServices is using ADSI behind the scenes and
because the data is coming out of a COM object or is it possible to see
this using objects that have only ever been .NET objects?


I don't know what ADSI will do, but you can certainly see the effect
with plain .NET.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #11
Okay, that's what I understood. I just got a bit confused by your
earlier statement. Thanks for clarifying it.

Nov 17 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: roni | last post by:
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..)
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
7
by: James Mcguire | last post by:
Hi, I frequently do non-initialisation type structure assignment via casting: e.g. struct s{int i,j,k;} mys; .... mys=(struct s){3,4,5};
18
by: Mike Bartels | last post by:
Hi Everyone! I have two Arrays A and B. Both arrays are byte arrays with 7 bytes each. The contents of array A and B are the same A = {1, 2, 3, 4, 5, 6, 7}; B = {1, 2, 3, 4, 5, 6, 7}; When...
3
by: Mike Cooper | last post by:
Hello All! I am getting teh above error message on the following code: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim dgt As...
6
by: John-Arne Lillebø | last post by:
Hi. I run into this problem and i could need some help to solve it. The project is an ASP.NET Web project. Including code sample of the problem. Any idea what is causing the error message ?...
9
by: Ben | last post by:
Hello, I'm not a developper, so sorry if it's a stupid question... I'm trying to develop an application in vb.net and I have the following problem: I have some information in an array:...
5
by: Jimp | last post by:
Why can't I cast List<MyObject> to ICollection<IMyObject>. MyObject implements IMyObject, and of course, List implements ICollection. Thanks
9
by: =?Utf-8?B?U0FM?= | last post by:
Is it possible to Cast an Object to a String Array the way I'm doing it in the code below? I'm trying to reuse code I have without creating new code if I can. Dim asNames() As String Dim...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.