Casting a ClassA[] to a ClassB[]

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • c

    #1

    Casting a ClassA[] to a ClassB[]

    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.ToArr ay(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

  • Jay B. Harlow [MVP - Outlook]

    #2
    Re: Casting a ClassA[] to a ClassB[]

    Con,[color=blue]
    > I realise that I can iterate over cB_array copying the elements
    > manually, but I'm trying (now desperately! :) ) to avoid that.[/color]
    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" <c@no.spam> wrote in message news:3f9d3c48$1 @usenet01.boi.h p.com...[color=blue]
    > 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.ToArr ay(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
    >[/color]


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Casting a ClassA[] to a ClassB[]

      c <c@no.spam> wrote:

      <snip>
      [color=blue]
      > 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."[/color]

      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[i] = (ClassA) cB_array[i];

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • c

        #4
        Re: Casting a ClassA[] to a ClassB[]

        Thank you Jay and Jon very much for your help.
        [color=blue]
        > 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.)[/color]

        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

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Casting a ClassA[] to a ClassB[]

          c <c@no.spam> wrote:[color=blue][color=green]
          > > 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.)[/color]
          >
          > 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.[/color]

          Yup, that's right.
          [color=blue]
          > 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".[/color]

          Exactly.
          [color=blue]
          > 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! :)[/color]

          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 :(

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          Working...