Monday, March 24, 2008

Implicit conversion from 'System.Array' to '1-dimensional array of Byte' in copying the value of 'ByRef' parameter 'Value' back to the matching argument

I converted a project from VB6 to VB2003 to VB2005.

In VB6 we had the following type of code:

      Dim tempBytes() As Byte, readFileHandle As Long
      readFileHandle = FreeFile
      Open imgFile For Binary Access Read As #readFileHandle
      ReDim tempBytes(1 To LOF(readFileHandle))
      Get #readFileHandle, 1, tempBytes 'dataoffset is 0-based; GET is 1-based

What this basically did was it opened an image file and then read in the image file to a byte array.

When we converted to VB2003, the code was converted and fixed as such:
        Dim tempBytes() As Byte
        Dim readFileHandle As Integer
        readFileHandle = FreeFile()
        FileOpen(readFileHandle, imgFile, OpenMode.Binary, OpenAccess.Read, OpenShare.Shared)         '!!## errors?
        ReDim tempBytes(LOF(readFileHandle) - 1)           
        FileGet(readFileHandle, tempBytes, 1)            'dataoffset is 0-based; GET is 1-based

In converting it to VB2005, I decided to turn Option Strict ON, which gave a warning on the following line.  The warning is the subject of this post:
        FileGet(readFileHandle, tempBytes, 1)            'dataoffset is 0-based; GET is 1-based

The warning made sense, tempBytes is type Byte, and FileGet is expecting a System.Array.

The way to fix this is to cast tempBytes as an array, and the warning will go away, and it will still work.
            FileGet(readFileHandle, DirectCast(tempBytes, Array), 1)            'dataoffset is 0-based; GET is 1-based




No comments: