Reading Structures from File

Z

Zahid

Hi,

I have declared an array holding a custom declared
structure. The structure looks like this:

Private Structure MnuDataFrmFile
Public menuGroup As String
Public ItemDesc As String
Public ItemPrice As Integer
Private PluValue As Integer
Private rgbR As Integer
Private rgbG As Integer
Private rgbB As Integer
Private buttonSize As String
End Structure

Array of Structure:
Dim HoldsAllItems() As MnuDataFrmFile

I want to read out from a file one line at a time. Each
line will populate the structure eaxactly. Currently Im
reading out from the file using:

Dim Item As String
Dim StreamToDisplay As StreamReader
StreamToDisplay = New StreamReader
("c:\allitems.txt")
Item = StreamToDisplay.ReadLine()

The problem is that each line is read as a string from
the file. My Structure has differing data types such as
string and integers.

How can I read a line from the file and populate the
Structure with the line split into the elements of the
structure (hence the correct datatypes)?

Urgent, Please help.

Thanks in advance
 
P

Peter Foot [MVP]

How about writing the details to a comma separated values (csv) text file
and then using the String.Split method with the comma character to convert
each row into an array of strings (one for each field) then assign them to
each field of your structure (using Int32.Parse to convert the Integer
values back to integer types)

Peter

--
Peter Foot
Windows Embedded MVP

In The Hand
http://www.inthehand.com
Handheld Interactive Reference Guides
 
Z

Zahid

Hi

Thanks for your answer. So sorry... my data is a comma
seperated file. How do you use the parse on the Integer?
This is where im stuck. How do I assign each array
element to the Structure?

Sorry, but im new to VB.Net.

Thanks in advance.
 
P

Peter Foot [MVP]

If your Item string contains the entire row, your next step would be to
create an array which contains each of the comma separated elements.
Dim ItemElements As String()

ItemElements = Item.Split(",")

You can parse an integer from a string using

anint = Integer.Parse(astring)

Based on your code some of the members of your struct are private and so you
cannot assign to them directly but you could add a constructor to create a
new MnuDataFrmFile from your item string - e.g.

Private Structure MnuDataFrmFile

Public menuGroup As String

Public ItemDesc As String

Public ItemPrice As Integer

Private PluValue As Integer

Private rgbR As Integer

Private rgbG As Integer

Private rgbB As Integer

Private buttonSize As String



Public Sub New(ByVal Item As String)

Dim ItemElements As String()

ItemElements = Item.Split()

menuGroup = ItemElements(0)

ItemDesc = ItemElements(1)

ItemPrice = Integer.Parse(ItemElements(2))

PluValue = Integer.Parse(ItemElements(3))

rgbR = Integer.Parse(ItemElements(4))

rgbG = Integer.Parse(ItemElements(5))

rgbB = Integer.Parse(ItemElements(6))

buttonSize = ItemElements(7)

End Sub

End Structure

note however this is a fairly rough-and-ready example - if there are any
errors in the file or values which cannot be parsed an exception will be
thrown. You would need to add additional checks to the code to handle
problems and decide how to handle them - abort the process, insert default
values etc

Peter


--
Peter Foot
Windows Embedded MVP

In The Hand
http://www.inthehand.com
Handheld Interactive Reference Guides
 

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