How to read and write a structure to a file with Option Strict ON

A

active

RecIn is a structure

FileGet(FileNum, RecIn, mNumb)

works if I have

Option Strict Off

File was written by

FilePut(FileNum, RecIn, mNumb)

So I guess there must be a Ctype that works

I haven't been able to find it so that I can use

Option Strict On

I also tried FileGetObject

and FilePutObject without success.



Do you know how to read and write a structure to a file with

Option Strict ON



thanks
 
H

Herfried K. Wagner [MVP]

active said:
RecIn is a structure

FileGet(FileNum, RecIn, mNumb)

works if I have

Option Strict Off

File was written by

FilePut(FileNum, RecIn, mNumb)

So I guess there must be a Ctype that works

I haven't been able to find it so that I can use

Option Strict On

Check out the sample below:

\\\
Option Compare Binary
Option Explicit On
Option Strict On

Public Class MainForm
Private Sub ButtonWriteRecord_Click( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles ButtonWriteRecord.Click
Dim p As New Person()
p.Birthday = #2/22/1987#
p.ShoeSize = 24
p.FirstName = "John"
p.LastName = "Doe"
p.Sex = Sex.Male
FileOpen( _
1, _
"People.dat", _
OpenMode.Binary, _
OpenAccess.Write _
)
FilePut(1, p)
FileClose(1)
End Sub

Private Sub ButtonReadRecord_Click( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles ButtonReadRecord.Click
FileOpen( _
1, _
"People.dat", _
OpenMode.Binary, _
OpenAccess.Read _
)
Dim p As ValueType = New Person()
FileGet(1, p)
MsgBox(p.ToString())
FileClose(1)
End Sub
End Class

Public Enum Sex
Male
Female
End Enum

Public Structure Person
Public FirstName As String
Public LastName As String
Public Birthday As Date
Public ShoeSize As Integer
Public Sex As Sex

Public Overrides Function ToString() As String
Return _
"FirstName={" & Me.FirstName & "}, " & _
"LastName={" & LastName & "}, " & _
"Birthday={" & Me.Birthday.ToString() & "}, " & _
"ShoeSize={" & Me.ShoeSize & "}, " & _
"Sex={" & Me.Sex.ToString() & "}"
End Function
End Structure
///
 
A

active

I can't imagine how you do that.

Thanks

Herfried K. Wagner said:
Check out the sample below:

\\\
Option Compare Binary
Option Explicit On
Option Strict On

Public Class MainForm
Private Sub ButtonWriteRecord_Click( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles ButtonWriteRecord.Click
Dim p As New Person()
p.Birthday = #2/22/1987#
p.ShoeSize = 24
p.FirstName = "John"
p.LastName = "Doe"
p.Sex = Sex.Male
FileOpen( _
1, _
"People.dat", _
OpenMode.Binary, _
OpenAccess.Write _
)
FilePut(1, p)
FileClose(1)
End Sub

Private Sub ButtonReadRecord_Click( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles ButtonReadRecord.Click
FileOpen( _
1, _
"People.dat", _
OpenMode.Binary, _
OpenAccess.Read _
)
Dim p As ValueType = New Person()
FileGet(1, p)
MsgBox(p.ToString())
FileClose(1)
End Sub
End Class

Public Enum Sex
Male
Female
End Enum

Public Structure Person
Public FirstName As String
Public LastName As String
Public Birthday As Date
Public ShoeSize As Integer
Public Sex As Sex

Public Overrides Function ToString() As String
Return _
"FirstName={" & Me.FirstName & "}, " & _
"LastName={" & LastName & "}, " & _
"Birthday={" & Me.Birthday.ToString() & "}, " & _
"ShoeSize={" & Me.ShoeSize & "}, " & _
"Sex={" & Me.Sex.ToString() & "}"
End Function
End Structure
///
 
R

redear

I can't imagine how you do that.

Thanks
Maybe this sample code will help:

' Define the structure (following is example only).
Public Structure RecIn
Public Item1 as Integer
Public Item2 as Integer
End Structure

' To read the structure, dim a variable
' as a ValueType = New RecIn.
Dim r as ValueType = New RecIn
FileGet(FileNum, r, mNumb)

' To then access fields in the structure,
' cast r from ValueType back to Recln.
MsgBox(CType(r, RecIn).Item1 + CType(r, RecIn).Item2)


HTH

redear
 
A

active

What I meant by the above is how does he comes up with the complete
code example so often. I see how it can mean something else.


Maybe this sample code will help:

Thanks a lot for helping
I pasted this example in my code as documentation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top