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
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!
![Smile :) :)](/styles/default/custom/smilies/smile.gif)
Thanks in advance for any help you can offer.
Con