Results 1 to 7 of 7

Thread: Option Strict & User defined variables

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2004
    Posts
    8

    Option Strict & User defined variables

    I have a problem using Option strict with user defined variables. I have a variable ModelInfo which is defined with a structure statement i.e

    Public Structure ModelInfo
    ….
    ….
    ….
    End Structure

    The variable is declared and is stored in a random access file i.e.

    Public ModelData as ModelInfo
    FileOpen(1, FileName, OpenMode.Random, , , Len(ModelData))

    The FilePut and FileGet statements work OK with Option Strict Off, however with Option Strict On the following FileGet statement returns an error.

    FileGet(1, ModelData, 1)

    Can anyone advise why this is?

    Thanks

  2. #2
    Addicted Member
    Join Date
    Sep 2004
    Location
    UK
    Posts
    129

    Re: Option Strict & User defined variables

    Hi, I got rid of the error message by:

    FileGet(1, CType(ModelData, ValueType), 1)


    I have no experience of FileGet, so I don’t know if this will give the correct results!

    Take a close look at the last part of the error message and you can see the problem

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2004
    Posts
    8

    Re: Option Strict & User defined variables

    Hi,

    FileGet(1, CType(ModelData, ValueType), 1) unfortunately doesn't read the file correctly.

    The last part of the error message is "Disallows implicit conversion from System.Value Type to ModelInfo"

    Have Tried FileGet(1, CType(ModelData, ModelInfo), 1) but this doesn't read the file correctly either.

  4. #4
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Option Strict & User defined variables

    I think it has something to do with the available overload forms for FileGet ...

    I think the one you want is:

    Public Overloads Sub FileGet( _
    ByVal FileNumber As Integer, _
    ByRef Value As Object, _
    Optional RecordNumber As Integer = -1 _
    )

    Sorry I can't help you more, but you need to figure out the relationship between "Public Structure ModelInfo" and a system Object. The error occurs because the system doesn't recognize "Structure" as an Object, or any other variable type acceptable to FileGet overloads. I hope someone else can see what is going on here and will jump in and help.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  5. #5
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Option Strict & User defined variables

    Do a search on "object AND structure" on this forum. I found several articles on Structures.

    "Structures are a bad thing?" by Mendhak
    "Structures"
    "object not defined"
    etc.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Option Strict & User defined variables

    I ran across this exact problem yesterday... i just turned off option strict because it was for a conversion app I was writing...(convert our accounts from an old random access file to a SQL Server) and option strict just wasn't importaint for this little app... I do use option strict however in all real world apps...

    I also read that using this method is BAD, as in VB.NET random file access has HUGE perfomance issues, and VB6 actually can do random file access a lot faster...The recommended solution was to use streams and read the file in bytes and plug the data into your structure that way. A little more work initially, but well worth it in the long run

  7. #7
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Option Strict & User defined variables

    Kleinma:

    I think the correct response is not to turn Strict off, but to figure out how do this operation correctly. I believe that the answer is to not use a Structure, but to make "ModelInfo/ModelData" Classes and not Structures.

    The only time I have had to turn 'Strict' off was for a case of 'Late Binding', and I think I eventually even found a way around that.

    Here is a very primitive sample of using a class instead of a Structure:

    Code:
        Option Strict On
    Public Class ModelInfo
            Public FirstName As String
            Public StartDate As Date
        End Class
    
        Public myObject As System.Object
    
        Sub TEST()
            Dim ModelData As New ModelInfo
            ModelData.FirstName = "Test"
            ModelData.StartDate = Date.Now
            myObject = CObj(ModelData)
        End Sub
    At least this compiles. It seems to like the 'fake' ModelInfo structure as a System Object, and as such it should work with the FileGet Object type overload. Maybe? Give this approach a try p_c, and Good Luck!
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width