473,505 Members | 14,618 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Saving a Structure to a file?


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
12 2537
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
"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
* 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
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
"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
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
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
"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
> After disabling Option Strict, the code runs without an error, here.

??????

I do not understand this

:-)

Cor
Nov 20 '05 #10
"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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
7509
by: G-Factor | last post by:
Hi all I've just started learning about saving files. I got bit of a problem. The following code gives me an error about incompatible types. (Cannot covert from class character to char *). I...
0
942
by: Sam Fisher | last post by:
Hi @all I'm really new to programming, so please dont' lose patience. Now, I'm writing a VB .net application in which (as in most apps) the user shall setup a configuration (via checkboxes etc.)....
2
2100
by: steve | last post by:
Hello, I am trying to import an image file into a form. This would be a persons picture saved in the same directory for every unique record. I don't have any problems making an action button to...
1
8227
by: Lespaul36 | last post by:
I have an icon structure..so it is a structure with other structures in it. I need to save it to a binary file. I have done some googling..but not much luck. How can I do this? Also, will long...
1
1193
by: pmclinn | last post by:
I have a formated a structure as follows: <Serializable()>public structure stcOutPut private data as string private visi as string end structure Instances of this strucuture are stored in an...
7
16552
by: Kirk | last post by:
Hello, I am trying to find a VB.NET method that will allow me to save and read an application's settings in a separate file located on any drive, any folder. I have found a couple of methods...
4
7837
by: adamrobillard | last post by:
Hi, I have always used fopen and FILE* to load and save structures to file. I am trying to convert all the older code to use proper C++ calls... the following code works properly but I would...
6
8048
by: Eddie | last post by:
Hi all, I am displaying a number of reports, and giving the users an option to display them on the web or download them to Excel. If they want the Excel file, I just use the PHP header command...
5
1540
by: jc | last post by:
RE: Two Classes with the same Data Structure.. saving code? Inheriting a structure? I have two classes. One in Inherits System.Collections.CollectionBase, the other does not, but they both have...
0
7216
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7367
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5613
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5028
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4699
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
407
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.