ValueType arrays

I'm trying to copy a cutomized valuetype to string type.

string source[10];

// bcString is a ValueType that hold a string object and implicit

// conversion from string type to bcString

bcString target[10];

target = source;

I have tryed with the syntax

target = (bcString[])(System.Array)source;

but I get an execution error (cannot convert...)

Any hints

Thanks,

Felice Russo



Answer this question

ValueType arrays

  • legoman26

    Din't know it was a sealed class, guess we learn every day :)

  • jsin

    The only way this is possible is if bcString inherits from string. What you will have to do is copy each value one by one with a for loop.

  • RogerAu

    same here - there's so much to learn especially with the v2.0 now out and i didn't even scratch the surface of v1.0/1.1 .

  • JohnsonInBeijing

    Have you tried using the Array.Copy method

    Based on your example, you might find that Array.Copy(source, target, 10) give you what you need.

    Hope it helps.



  • cell-gfx

    ThE_lOtUs - I'm sure you already knew this but just so that the OP does not get confused you cannot derive from string since its a sealed class.

    Felice - like what others have suggested, you'll most likely have to do a item by item copy of the array. How is your type bcString defined Since you say it's a value type, is it a structure or enum You can neither derive from a specific value type nor from the ValueType class.

    hope that helps,
    Imran.

  • silvarea

    Hi Bruce,

    I have tried with Array.Copy but the MSDN Array.Copy documentation say:

    An ArrayTypeMismatchException is thrown if the arrays are of incompatible types. Type compatibility is defined as follows:

    ...

    - A nonintrinsic (user-defined) value type is compatible only with itself.

    In fact, I get this error on execution.

    Any additional hint


  • ValueType arrays