Why can't I read a structure from a file?

  • Thread starter Thread starter HONOREDANCESTOR
  • Start date Start date
H

HONOREDANCESTOR

I tried to read a structure from a file, but I got this error message
before I could compile it:
"Error 103 Option Strict On disallows narrowing from type
'System.ValueType' to type 'OptionsRecordType' in copying the value of
'ByRef' parameter 'Value' back to the matching argument."
The 3 lines of code are:
If My.Computer.FileSystem.FileExists(strPath) Then
FileOpen(1, strPath, OpenMode.Binary)
FileGet(1, OptionsRecord)
The third line is the one that causes the trouble. 'OptionsRecord' is
a structure with 2 integer fields.
I can't find any help on this in MSDN. If you can help, its certainly
appreciated.
 
I tried to read a structure from a file, but I got this error message
before I could compile it:
"Error 103 Option Strict On disallows narrowing from type
'System.ValueType' to type 'OptionsRecordType' in copying the value of
'ByRef' parameter 'Value' back to the matching argument."

So, why don't you set Option Strict Off, which is a property of the project?
 
I tried to read a structure from a file, but I got this error message
before I could compile it:
"Error 103 Option Strict On disallows narrowing from type
'System.ValueType' to type 'OptionsRecordType' in copying the value of
'ByRef' parameter 'Value' back to the matching argument."
The 3 lines of code are:
If My.Computer.FileSystem.FileExists(strPath) Then
FileOpen(1, strPath, OpenMode.Binary)
FileGet(1, OptionsRecord)
The third line is the one that causes the trouble. 'OptionsRecord' is
a structure with 2 integer fields.
I can't find any help on this in MSDN. If you can help, its certainly
appreciated.

You need to Dim a new instance of the structure as a valuetype,
FileGet into the instance, and then use a cast of the instance back to
the structure to access the integer field within the structure. Off
the top of my head, the important statements are something like these,
assuming IntField1 is the name of the first integer field in the
structure:

Dim opt as ValueType = New OptionsRecord
FileGet(1, opt) ' Works because opt is a valuetype
If (CType(opt, OptionsRecord).IntField1) = 1 then ' etc

HTH
 
Back
Top