Saving a Structure to a file?

  • Thread starter Thread starter imme929
  • Start date Start date
I

imme929

How do I do it? Nothing in the books is helpful.
I need to save a structure with different data types.
 
imme: Look on Google for VB.NET & Serializable. It's an attribute and you
can mark up your Structures or your classes with it. From there, you can
implement the serialization however you want, there are tons of examples out
there but treat it just like a class. You'll need to decide what format you
want it saved in (Binary, SOAP, XML, Text) and that will dictate how you
proceed.

HTH,

Bill
 
William said:
imme: Look on Google for VB.NET & Serializable. It's an attribute and you
can mark up your Structures or your classes with it. From there, you can
implement the serialization however you want, there are tons of examples out
there but treat it just like a class. You'll need to decide what format you
want it saved in (Binary, SOAP, XML, Text) and that will dictate how you
proceed.

I got things working until I tried this...

Public Enum Keyboard
EnglishUS
EnglishUK
Spanish
German
Italian
French
End Enum

Public Structure TUserStats
Public language As Keyboard <----------
Public User As String
Public BestPercentage As String
Public BestCompleted As Integer
Public BestAverage As Single
End Structure

The arrow above points to the problem. I'm stopped at
this line below:
FileGet(1, Globals.RecStats)

An error window pops up and says....
"An unhandled exception of type 'System.ArgumentException'
occurred in microsoft.visualbasic.dll"
"Additional information: Object type cannot be converted
to target type."

'..
 
imme929 said:
I got things working until I tried this...

Public Enum Keyboard
EnglishUS
EnglishUK
Spanish
German
Italian
French
End Enum

Public Structure TUserStats
Public language As Keyboard <----------
Public User As String
Public BestPercentage As String
Public BestCompleted As Integer
Public BestAverage As Single
End Structure

The arrow above points to the problem. I'm stopped at
this line below:
FileGet(1, Globals.RecStats)

An error window pops up and says....
"An unhandled exception of type 'System.ArgumentException'
occurred in microsoft.visualbasic.dll"
"Additional information: Object type cannot be converted
to target type."

'..


Are you sure you saved the same Structure in the file? Is it saved at the
same position as you read from? Can you show use the code how you wrote the
Structure to the file?


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Armin said:
Are you sure you saved the same Structure in the file? Is it saved at the
same position as you read from? Can you show use the code how you wrote the
Structure to the file?

The code below works fine until the 3 lines commented
are uncommented. then it stops with the error message.

Public Enum Keyboard
EnglishUS
EnglishUK
Spanish
German
Italian
French
End Enum

Public Enum TBorderColor
green
blue
red
purple
brown
End Enum

Public Structure TUserStats
'Public lang As Keyboard
Public User As String
Public BestPercentage As String
Public BestCompleted As Integer
Public BestAverage As Single
End Structure

Globals.FileStats.BestPercentage = "100 percent "
Globals.FileStats.BestCompleted = 99
Globals.FileStats.BestAverage = 0.8
'Globals.FileStats.lang = Globals.Keyboard.EnglishUS
Globals.FileStats.User = ""

FileOpen(1, "TESTFILE.data", OpenMode.Binary)
FilePut(1, Globals.FileStats)
FileClose(1)

Globals.FileStats.BestPercentage = "88 percent "
Globals.FileStats.BestCompleted = 9111
Globals.FileStats.BestAverage = 0.8888
'Globals.FileStats.lang = Globals.Keyboard.EnglishUS
Globals.FileStats.User = ""

Label2.Text = Globals.FileStats.BestPercentage
Label3.Text = Globals.FileStats.BestCompleted

FileOpen(1, "TESTFILE.data", OpenMode.Binary)
FileGet(1, Globals.FileStats)
FileClose(1)

Label1.Text = Globals.FileStats.BestPercentage
Label5.Text = Globals.FileStats.BestCompleted
Label4.Text = Globals.FileStats.BestAverage
 
Imme929,
Have you considered using System.IO.BinaryReader & BinaryWriter or .NET
Serialization?

Both which support enums in structures.

The "easiest" way to write a class or structure to a binary file is to use
Binary Serialization. Which is covered in the following 3 part article:

http://msdn.microsoft.com/msdnmag/issues/02/04/net/
http://msdn.microsoft.com/msdnmag/issues/02/07/net/
http://msdn.microsoft.com/msdnmag/issues/02/09/net/

If you don't want or need the overhead of Binary Serialization you can "roll
your own" using the BinaryReader & BinaryWriter classes found in System.IO.
Note with the BinaryReader & BinaryWriter you need to read & write
individual fields, not the entire class or structure at once.

The following article offers using a BinaryReader & BinaryWriter to
serializing a class to a NetworkStream, you can use a FileStream and have
the same effect.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp09182003.asp

The example is in C#, however it should be easily converted to VB.NET, post
if you need help.

Hope this helps
Jay
 
imme929 said:
Are you sure you saved the same Structure in the file? Is it saved
at the same position as you read from? Can you show use the code
how you wrote the Structure to the file?

The code below works fine until the 3 lines commented
are uncommented. then it stops with the error message.

Code:
[/QUOTE]

After disabling Option Strict, the code runs without an error, here.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
After disabling Option Strict, the code runs without an error, here.

??????

I do not understand this

:-)

Cor
 
Cor Ligthert said:
??????

I do not understand this

:-)

To solve the problem, I have to create the same conditions as the OP has. To
do this, I have to disable option strict. Even if I do it, there is no
error. This does not mean one should disable option strict, but it was the
only way to find out if the error can be reproduced.
 
Hi Armin,
To solve the problem, I have to create the same conditions as the OP has. To
do this, I have to disable option strict. Even if I do it, there is no
error. This does not mean one should disable option strict, but it was the
only way to find out if the error can be reproduced.

I did understand that of course, however I could not resist to give you this
answer in this case.

:-))

Cor
 
Cor Ligthert said:
I did understand that of course, however I could not resist to give
you this answer in this case.

:-))

I know how you meant it, but for the same reason I also couldn't resist to
give you a "serious" answer. :))
 
Back
Top