Casting a ClassA[] to a ClassB[]

C

c

Hello All,

I am struggling with simple casting issue within c# and would really
appreciate some insight into where I am going wrong.

I have two classes, ClassA and ClassB. Each implements a "public static
implicit operator" to allow silent casting from one to the other. This
works fine, per the documents I have read.

However, I am unable to do this:

ClassB[] cB_array = new ClassB[2];
ClassA[] cA_array = cB_array;

Can anybody offer a pointer to where I'm going wrong here?

I've tried using Array.Copy, Arraylist.ToArray(Type), explicit casting
with "(ClassA[]) cB_array", and presizing cA_array to have 2 elements -
all to no avail; I get either compile- or run-time errors.

Yet I have read in several documents that implicit casting can occur:

"From any array type A with element type a to an array type B with
element type b provided A & B differ only in element type (but the same
number of elements) and both a and b are reference types and an implicit
reference conversion exists between a & b."

I realise that I can iterate over cB_array copying the elements
manually, but I'm trying (now desperately! :) ) to avoid that.

Thanks in advance for any help you can offer.

Con
 
J

Jay B. Harlow [MVP - Outlook]

Con,
I realise that I can iterate over cB_array copying the elements
manually, but I'm trying (now desperately! :) ) to avoid that.
I believe this is your only options, as the 'public static implicit
operator' is something that C# understands and not the Framework itself per
se.

Which means that Array.Copy is not able to use the implicit operator.

I am wondering however, how generics will change this situation. Generics
will be available with the next release of .NET, aka Whidbey (VS.NET 2004).

Hope this helps
Jay

c said:
Hello All,

I am struggling with simple casting issue within c# and would really
appreciate some insight into where I am going wrong.

I have two classes, ClassA and ClassB. Each implements a "public static
implicit operator" to allow silent casting from one to the other. This
works fine, per the documents I have read.

However, I am unable to do this:

ClassB[] cB_array = new ClassB[2];
ClassA[] cA_array = cB_array;

Can anybody offer a pointer to where I'm going wrong here?

I've tried using Array.Copy, Arraylist.ToArray(Type), explicit casting
with "(ClassA[]) cB_array", and presizing cA_array to have 2 elements -
all to no avail; I get either compile- or run-time errors.

Yet I have read in several documents that implicit casting can occur:

"From any array type A with element type a to an array type B with
element type b provided A & B differ only in element type (but the same
number of elements) and both a and b are reference types and an implicit
reference conversion exists between a & b."

I realise that I can iterate over cB_array copying the elements
manually, but I'm trying (now desperately! :) ) to avoid that.

Thanks in advance for any help you can offer.

Con
 
J

Jon Skeet [C# MVP]

Yet I have read in several documents that implicit casting can occur:

"From any array type A with element type a to an array type B with
element type b provided A & B differ only in element type (but the same
number of elements) and both a and b are reference types and an implicit
reference conversion exists between a & b."

That is in the C# spec, but the implicit reference conversions they're
talking about there are those listed in section 13.1.4 (ECMA spec)
rather than the user-defined implicit conversions as described in
section 13.4.3. (If you look in the spec, there are links to the
appropriate other bits.)

In this case, you'll basically need to do:

ClassA[] cA_array = new ClassA[cB_array.Length];
for (int i=0; i < cB_array.Length; i++)
cA_array = (ClassA) cB_array;
 
C

c

Thank you Jay and Jon very much for your help.
That is in the C# spec, but the implicit reference conversions they're
talking about there are those listed in section 13.1.4 (ECMA spec)
rather than the user-defined implicit conversions as described in
section 13.4.3. (If you look in the spec, there are links to the
appropriate other bits.)

So. I see what is meant here now. I have looked at the relevent
sections in depth in the spec and I understand more what you are saying,
Jon.

Trying to convert 2 arrays, as I am, is an "implicit reference
conversion" (13.1.4) and I have created a "user-defined implicit
conversion" (13.4.3) between my classes.

Whilst these are both defined as types of "implicit conversions" (13.1),
the spec says that one of the conditions that must be met to convert an
array like this is that an "implicit reference conversion" exists
between the types in the arrays and these are not the same as
"user-defined implicit conversions".

Good grief. Maybe what I've been trying to do isn't that common but I'm
amazed that I haven't seen this problem/solution mentioned in other
documents or in other postings. Even reading the spec didn't make it
immediately obvious! :)

Thanks again.

Con
 
J

Jon Skeet [C# MVP]

c said:
So. I see what is meant here now. I have looked at the relevent
sections in depth in the spec and I understand more what you are saying,
Jon.

Trying to convert 2 arrays, as I am, is an "implicit reference
conversion" (13.1.4) and I have created a "user-defined implicit
conversion" (13.4.3) between my classes.

Yup, that's right.
Whilst these are both defined as types of "implicit conversions" (13.1),
the spec says that one of the conditions that must be met to convert an
array like this is that an "implicit reference conversion" exists
between the types in the arrays and these are not the same as
"user-defined implicit conversions".
Exactly.

Good grief. Maybe what I've been trying to do isn't that common but I'm
amazed that I haven't seen this problem/solution mentioned in other
documents or in other postings. Even reading the spec didn't make it
immediately obvious! :)

Yup - it's one of those things where the spec isn't as clear as it
might be, because it defines two things in very similar language -
which is fine most of the time, as they're very similar things, but
they're *just* about different enough to make a huge difference in this
case :(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top