Casting arrays

  • Thread starter Thread starter Lews
  • Start date Start date
L

Lews

Guest
An external program calls a fixed operation on my application. That is, my application implements the method of the other program, so the method can not be changed.

My problem is that one of the parameters that my application receives from the external program is an "object addresses".

This object is in fact a "object[] addresses", and to be more specific its a "string[] addresses".

However when my application receives it it thinks that its of the type "object addresses".

My problem is now that I cant access the elements in "addresses" because its not declared as an array... and if I try to do something like this

object[] addr;

addr = (object[]) addresses;

the c#-compiler complains because its not a valid cast.

So my question is, how do I access the strings in the addresses object?



Thanks in advance :rolleyes:
 
Im a little confused. You say that addresses is not an array yet above you have object[] addresses which means that it is. You also have object[] addr below which is an array of objects and attempting to reference the addresses array with it. That seems redundant to me since an object can accept anything in .NET. Wouldnt it make more sense to declare addr as an array of strings?

Regardless of all that, you cant cast an entire array. You have to cast the individual elements..

addr[#] = (object) addresses[#];

But if the addresses array is a string you can just simply access the elements with addresses[#].ToString();
 
Actually the code I wrote did solve my problem. I tried a lot of different combinations to write it but the one I wrote above I had not tested. I tried to cast it to a string array instead of an object array.

And to clear things out. If you have an object[] addrArray I think you can type like this:

object adressses;
adresses = addrArray;

Which means that you put an array as an object. To revert this operation you write as I wrote above

object[] addr;
addr = (object[]) addresses;

My problem was only the last part of this since I got an object addresses as input from an external method I implemented which actually was an object[] addrArray.



Does it make any sense? :).
 
Ah, hrm weird I tried casting an entire array and got an error. Probably did something wrong like I always do.

Anyway.. so the question is then how do you get a String value out of an Object? Well calling the ToString property should work (Object.ToString), other then that you should be able to use the object as is for the String value as well ("Some string " + Object).
 

Similar threads

P
Replies
0
Views
176
Policy standard local admin account with Active Di
P
Back
Top