473,379 Members | 1,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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

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
Feb 23 '07 #1
5 2616
Do you have to serialize it or something?

" active" <ac**********@a-znet.comwrote in message
news:uO**************@TK2MSFTNGP04.phx.gbl...
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


Feb 23 '07 #2
" active" <ac**********@a-znet.comschrieb:
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
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Feb 23 '07 #3
I can't imagine how you do that.

Thanks

"Herfried K. Wagner [MVP]" <hi***************@gmx.atwrote in message
news:eZ*************@TK2MSFTNGP06.phx.gbl...
>" active" <ac**********@a-znet.comschrieb:
>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
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Feb 25 '07 #4
On Sat, 24 Feb 2007 19:00:37 -0500, " active"
<ac**********@a-znet.comwrote:
>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
Feb 25 '07 #5
<ac**********@a-znet.comwrote:
>>I can't imagine how you do that.
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
>
' 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


Feb 25 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michael Van Altena via .NET 247 | last post by:
I'm trying to figure out how to read a formatted binary file intoa structure definition in C#. I've tried using the"StructLayout" attribute with both LayoutKind.Explicit andLayoutKind.Sequential...
3
by: Dae-Suk Chung | last post by:
Hi, What is the easiest way to write a struct(with biary data) to a file ? do I need to covert it through e.g. Marshal class ? Thanks for any tips! DaeSuk.
12
by: imme929 | last post by:
How do I do it? Nothing in the books is helpful. I need to save a structure with different data types.
4
by: Dave Cullen | last post by:
I want to call a function using my own data type as an argument. The declaration for my function looks like this: Public Function CreateCardInfo(ByVal data As WoData) VB.NET balks at this,...
5
by: BobC | last post by:
Article ID: 301225 I can't seem to get this code to work. I modified it alittle so I could try and figure out the exception. I keep getting an error on the " Do While (reader.Read()) " statement....
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
7
by: Tracks | last post by:
I have old legacy code from vb5 where data was written to a file with a variant declaration (this was actually a coding error?)... in vb5 the code was: Dim thisdata as integer Dim thatdata...
2
by: HONOREDANCESTOR | last post by:
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...
11
by: Icemokka | last post by:
Hi, I'm need to upload a big file ( 600Mb+ ) to a BLOB field in MSSQL 2005. My code looks like this : fs = New FileStream(sFilePath, FileMode.Open) Dim ByteArray(fs.Length) As Byte...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.