none
Why would converting from a type to the same type be potentially problematic? RRS feed

  • Question


  • Resharper says this: 

    "Co-variant array conversion from Device[] to Device[] can cause run-time exception on write operation"

    ...about this code:

    symbolDevice = SelectDevice.Select(Symbol.Barcode.Device.Title, Symbol.Barcode.Device.AvailableDevices);




    Why is converting from Device[] to Device[] supposedly dangerous?
    Wednesday, October 2, 2013 9:19 PM

Answers

  • It doesn't have to do something with custom properties or implementaion details of the Device class, its just a formal thing.
    Given this scenario:
    abstract class B{}
    class D1 : B{}
    class D2 : B{}
    If you have an array of type D1[] and you cast it to B[] than you could do an write operation like arr[0] = new D2().
    So you could assign a D2 instance to an array that actually is an array of D1.
    But this means that there is something wrong with the in/out modifiers because it should be impossible to down cast a writable collection to its base type collection, to prevent this write operation errors.

    Chris


    Thursday, October 3, 2013 1:15 PM

All replies

  • You could have two different bar code readers from different manufactures.  Each manufacture could be using the same base "Class Device" but adding different custom properties by inheriting the base class.

    jdweng

    Wednesday, October 2, 2013 9:53 PM
  • In this case, though, Resharper does not offer any solutions for fixing it. Does anybody have an idea how this code has to be modified to make it safe?
    Wednesday, October 2, 2013 10:41 PM
  • The comment Resharper is making about converting from device[] to device[] on the line you posted I don't think applies since you are not doing a write.  The line can give errors for a different reason.  Deices don't always impliment a title property.  So you may not get a valid response using the title property and need to check that the device exists.

    I suspect the device is using a USB port.  Some USB devices don't use a title expecially older USB devices.  Title sometimes contain serialnumber of the device so where you may have multiple USB devices of the same type you can distinquish between the devices.  Also with USB devices that don't have a serial number yo uhave issues when the device is moved from one USB connector to another USB connector.  The PC usually thinks a new USB device is being connected and reloads the driver.  this can create issues when the User doesn't have admin credentials and can't install devices.


    jdweng

    Thursday, October 3, 2013 2:17 AM
  • It doesn't have to do something with custom properties or implementaion details of the Device class, its just a formal thing.
    Given this scenario:
    abstract class B{}
    class D1 : B{}
    class D2 : B{}
    If you have an array of type D1[] and you cast it to B[] than you could do an write operation like arr[0] = new D2().
    So you could assign a D2 instance to an array that actually is an array of D1.
    But this means that there is something wrong with the in/out modifiers because it should be impossible to down cast a writable collection to its base type collection, to prevent this write operation errors.

    Chris


    Thursday, October 3, 2013 1:15 PM