473,471 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Casting struct[] to object[]

Hi
I cannot figure why it isn't possible to cast a struct array to an object
array.

I written a structure like this:

public struct Test {
private int TestA;
private int TestB;

public int A {
get {return this.TestA;}
}
public int B {
get {return this.TestB;}
}
public Test(int a, int b) {
this.TestA=a;
this.TestB=b;
}
}

I have an array of Test struct values (Test[]):
Test[] = new my_struct[2];
my_struct[0] = new Test(1,2);
my_struct[1] = new Test(2,3);

but if I try to cast the structure array to the object:
object[] dest = (object[])my_struct;

the compiler tells me that it isn't possible to cast Test[] to object[],
although it gives me no error if I cast a single Test struct value to
object:
object o = (object)my_struct[0];

Moreover, if i change the struct declaration to a class declaration, no
error occurs for a class array to an object array!

Should I perform some casting operator overload in Test?
TIA
Fabrizio

Nov 16 '05 #1
4 16259
Try this

Test[] my_struct= new Test[2];
my_struct[0] = new Test(1,2);
my_struct[1] = new Test(2,3);

object[] dest = new object[my_struct.Length];
int count = 0;
foreach(Test val in my_struct)
{
dest[count] = new object();
dest[count] = (object)val;
count++;
}

--
Shak
(Houston)
"Fabrizio" <no****@nospam.com> wrote in message
news:d1********************@twister2.libero.it...
Hi
I cannot figure why it isn't possible to cast a struct array to an object
array.

I written a structure like this:

public struct Test {
private int TestA;
private int TestB;

public int A {
get {return this.TestA;}
}
public int B {
get {return this.TestB;}
}
public Test(int a, int b) {
this.TestA=a;
this.TestB=b;
}
}

I have an array of Test struct values (Test[]):
Test[] = new my_struct[2];
my_struct[0] = new Test(1,2);
my_struct[1] = new Test(2,3);

but if I try to cast the structure array to the object:
object[] dest = (object[])my_struct;

the compiler tells me that it isn't possible to cast Test[] to object[],
although it gives me no error if I cast a single Test struct value to
object:
object o = (object)my_struct[0];

Moreover, if i change the struct declaration to a class declaration, no
error occurs for a class array to an object array!

Should I perform some casting operator overload in Test?
TIA
Fabrizio


Nov 16 '05 #2
Keep in mind that your line of code:

object o = (object)my_struct[0];

is actually making a copy of the struct and then boxing it within an object
wrapper. So, to cast an array of struct (or any non-reference type such as
int, long, etc.) to an array of object would require the system to allocate
a whole new array and copy/box every element of the source array into it.
Unlike the code which casts an array of reference types to an array of
objects, the two array references in our case would actually point to two
completely different arrays.

Since this is not semantically what you would expect and could be very
costly, the system simply doesn't allow it. If you want to copy it to an
array of objects, you'll have to do it manually.

Ken
"Fabrizio" <no****@nospam.com> wrote in message
news:d1********************@twister2.libero.it...
Hi
I cannot figure why it isn't possible to cast a struct array to an object
array.

I written a structure like this:

public struct Test {
private int TestA;
private int TestB;

public int A {
get {return this.TestA;}
}
public int B {
get {return this.TestB;}
}
public Test(int a, int b) {
this.TestA=a;
this.TestB=b;
}
}

I have an array of Test struct values (Test[]):
Test[] = new my_struct[2];
my_struct[0] = new Test(1,2);
my_struct[1] = new Test(2,3);

but if I try to cast the structure array to the object:
object[] dest = (object[])my_struct;

the compiler tells me that it isn't possible to cast Test[] to object[],
although it gives me no error if I cast a single Test struct value to
object:
object o = (object)my_struct[0];

Moreover, if i change the struct declaration to a class declaration, no
error occurs for a class array to an object array!

Should I perform some casting operator overload in Test?
TIA
Fabrizio


Nov 16 '05 #3
First of all, conversion from struct to object isn't a cast. It's boxing.
Second, you don't want to cast the array reference you have to, say
System.Array or System.Object, you want to cast every item in the array.
That's more than a cast.
However, the .net language designers thought of that and build a method for
it: Array.Copy.

object[] dest = new object[my_struct.Length];
Array.Copy(my_struct, dest, my_struct.Length);

Copies (and casts) data from one array to another. Fine, isn't it?

Niki

"Fabrizio" <no****@nospam.com> wrote in
news:d1********************@twister2.libero.it...
Hi
I cannot figure why it isn't possible to cast a struct array to an object
array.

I written a structure like this:

public struct Test {
private int TestA;
private int TestB;

public int A {
get {return this.TestA;}
}
public int B {
get {return this.TestB;}
}
public Test(int a, int b) {
this.TestA=a;
this.TestB=b;
}
}

I have an array of Test struct values (Test[]):
Test[] = new my_struct[2];
my_struct[0] = new Test(1,2);
my_struct[1] = new Test(2,3);

but if I try to cast the structure array to the object:
object[] dest = (object[])my_struct;

the compiler tells me that it isn't possible to cast Test[] to object[],
although it gives me no error if I cast a single Test struct value to
object:
object o = (object)my_struct[0];

Moreover, if i change the struct declaration to a class declaration, no
error occurs for a class array to an object array!

Should I perform some casting operator overload in Test?
TIA
Fabrizio


Nov 16 '05 #4
Fabrizio <no****@nospam.com> wrote:
I cannot figure why it isn't possible to cast a struct array to an object
array.


Think about how they're both in memory. In an object[], there's a list
of references, one after another, at 4-byte intervals (on x86, anyway).

Your struct takes up 8 bytes - so the array of structs occurs with one
at every 8 bytes.

Now put boxing in as well - your "cast" would actually have to create a
new array, box every element in the original and put a reference to
each boxed value into the new array. Nasty!

The reason it works when you cast (say) MyClass[] to object[] is that
none of that needs to happen - an array of MyClass references *is* an
array of object references already. (The only problem is if you then
try to say foo[0] = new object() - you'll get a runtime exception.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

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

Similar topics

4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
13
by: JustSomeGuy | last post by:
I have two object types ClassA and ClassB class ClassA { public: int data; operator ClassB() { ClassB b; b.data = data + 1; return (b);
3
by: Rob Jackson | last post by:
HiI've got a struct, known by file A.c, which contains a pointer to struct B. Struct B is unknown by file A.c (it is declared in C.h), and contains a typedef enum, which is declared in a file B.h,...
1
by: rahul8143 | last post by:
hello, In kernel source code there is ip_fragment.c file my question is regarding pointer function and casting for that look at required snippet from that file There is structure defined for...
44
by: Agoston Bejo | last post by:
What happens exactly when I do the following: struct A { int i; string j; A() {} }; void f(A& a) { cout << a.i << endl;
8
by: wkaras | last post by:
In my compiler, the following code generates an error: union U { int i; double d; }; U u; int *ip = &u.i; U *up = static_cast<U *>(ip); // error I have to change the cast to...
3
by: LongBow | last post by:
Hello all, First of all, sorry for multiple question per one thread. I have two questions. First what I think might be the easier problem. I am capturing data from an embedded device which I...
8
by: Neha | last post by:
Hi all Its seems bit silly que but pleasee explan me why this error is coming ? and what will be the solution if i want void * to be intilaize by struct * and this code is puerly in C. --...
9
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
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
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...
1
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
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,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.