By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,921 Members | 1,479 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,921 IT Pros & Developers. It's quick & easy.

Saving a Structure to a file?

P: n/a

How do I do it? Nothing in the books is helpful.
I need to save a structure with different data types.

Nov 20 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
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
"imme929" <im*****@nosuchplace.net> wrote in message
news:Cx**************@newssvr15.news.prodigy.com.. .

How do I do it? Nothing in the books is helpful.
I need to save a structure with different data types.

Nov 20 '05 #2

P: n/a
"imme929" <im*****@nosuchplace.net> schrieb

How do I do it? Nothing in the books is helpful.
I need to save a structure with different data types.

http://msdn.microsoft.com/library/en...Processing.asp
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #3

P: n/a
* imme929 <im*****@nosuchplace.net> scripsit:
How do I do it? Nothing in the books is helpful.
I need to save a structure with different data types.


'FilePut' -> F1.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #4

P: n/a
William Ryan eMVP wrote:
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."

'..
Nov 20 '05 #5

P: n/a
"imme929" <im*****@nosuchplace.net> schrieb
William Ryan eMVP wrote:
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."

'..

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

Nov 20 '05 #6

P: n/a
Armin Zingler wrote:
"imme929" <im*****@nosuchplace.net> schrieb
William Ryan eMVP wrote:
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."

'..


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
Nov 20 '05 #7

P: n/a
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/de...rp09182003.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" <im*****@nosuchplace.net> wrote in message
news:XG*******************@newssvr31.news.prodigy. com...
Armin Zingler wrote:
"imme929" <im*****@nosuchplace.net> schrieb
William Ryan eMVP wrote:

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."

'..


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

Nov 20 '05 #8

P: n/a
"imme929" <im*****@nosuchplace.net> schrieb

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]


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

Nov 20 '05 #9

P: n/a
> After disabling Option Strict, the code runs without an error, here.

??????

I do not understand this

:-)

Cor
Nov 20 '05 #10

P: n/a
"Cor Ligthert" <no**********@planet.nl> schrieb
After disabling Option Strict, the code runs without an error,
here.


??????

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.
--
Armin

Nov 20 '05 #11

P: n/a
Hi Armin,
After disabling Option Strict, the code runs without an error,
here.
??????

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.


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

:-))

Cor
Nov 20 '05 #12

P: n/a
"Cor Ligthert" <no**********@planet.nl> schrieb
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.

:-))


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

Nov 20 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.