explicit operator overload for arrays

I have two classes, Person and Reporter. I created, in the Reporter class, explicit operator overloads going both ways, Person to Reporter and Reporter to Person, and it all works as designed. I can explicitly cast:

Person p = new Person();

Reporter r = (Reporter)p;

// works great!

Now I want to do this:

Person[] people = new Person[50];

// some code here to populate the Person array with Person objects, then:

Reporter[] reporters = (Reporter[])people;

// Crashes. I've changed the code so now I can't get the exact error but it is

//something about user defined conversions must be to or from the enclosed type.

I tried creating:

public static explicit override Reporter[](Person[] people)

{

// some code to iterate the Person array and do a deep copy

// to a Reporter array

}

and I got the same error about conversions must be to or from the enclosed type.

Does anyone know if there's a way to create an explicit conversion from one array type to another

Thanks,

Dale Preston

MCAD,MCSE, MCDBA



Answer this question

explicit operator overload for arrays

  • chaddyb

    For purposes of this question, reporter does not inherit from person.

    Generics won't work. They won't convert or cast from one type to another and I have two different classes that are related and convertable individually. And the application that led me to this question is v1.1.

    Dale


  • Zaerion

    No there isn't. You'll have to assign each array element in a loop.



  • Daniel Ch. Bloch

    And reporter inherits from person right

    Also take a look at generics



  • aw52741

    That figures. Well, I don't like arrays anyway. I'll create the custom collection I knew I should have anyway. Then I can create an explicit operator override converting the array type to the custom collection.

    Thanks for your help.

    Dale


  • explicit operator overload for arrays