|
-
Jul 22nd, 2012, 06:51 PM
#1
Thread Starter
Lively Member
Reading from text file and storing into an array
Hi, I am given a problem where I have a text file that contains the following:
Code:
12AVX
5
23ABC
8.97
23TWT
4.69
34ZAB
12.5
91BAN
34.67
Some of the instructions include "Define a structure named Product. The structure should contain two member variables: a String variable to store the item number and a Double variable to store the price" and "Declare a class-level array that contains five Product structure variables."
I am stuck on the following "The form’s Load event procedure should read the item numbers and prices from the ItemInfo.txt fi le and store them in the classlevel array. It also should add the item numbers to the list box."
Code:
Option Explicit On
Option Strict On
Option Infer Off
Public Class frmMain
Private ProductInfo(4) As Product
Structure Product
Public strId As String
Public dblPrice As Double
End Structure
Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim inFile As IO.StreamReader
Dim intArray As Integer
Dim intLength As Integer
Dim strTemp As String
' verify that the file exists
If IO.File.Exists("ItemInfo.txt") = True Then
' open the file for input
inFile = IO.File.OpenText("ItemInfo.txt")
'process loop instructions until end of
Do Until inFile.Peek = -1
strTemp = inFile.ReadLine
Integer.TryParse(strTemp, intLength)
If intLength = 5 Then
ProductInfo(intArray).strId = strTemp
Else
Double.TryParse(strTemp, ProductInfo(intArray).dblPrice)
End If
' add the line to the list box
lstNumbers.Items.Add(ProductInfo(intArray).strId)
intArray = intArray + 1
Loop
' close the file
inFile.Close()
' select the first line in the list box
lstNumbers.SelectedIndex = 0
End If
End Sub
End Class
When I run this code my list box is empty and I am not sure where I went wrong. Any help would be appreciated.
-
Jul 22nd, 2012, 07:41 PM
#2
Re: Reading from text file and storing into an array
try this:
vb Code:
Public Class Form1
''' <summary>
''' Product Structure
''' </summary>
''' <remarks>contains a string member + a double member.
''' overriding the ToString, determines what is displayed in the listbox when you add a Product</remarks>
Private Structure Product
Dim itemNumber As String
Dim price As Double
Public Overrides Function ToString() As String
Return Me.itemNumber
End Function
End Structure
''' <summary>
''' products array
''' </summary>
''' <remarks>5 element array of Product</remarks>
Dim products(4) As Product
''' <summary>
''' Form1_Load event
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks>here we read the text file, then loop through the lines + load the products array + add products to listbox</remarks>
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim lines() As String = IO.File.ReadAllLines("fileName.txt")
Dim counter As Integer = 0
For x As Integer = 0 To lines.GetUpperBound(0) Step 2
products(counter).itemNumber = lines(x)
products(counter).price = CDbl(lines(x + 1))
ListBox1.Items.Add(products(counter))
counter += 1
Next
End Sub
''' <summary>
''' ListBox1_SelectedIndexChanged event
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks>this shows how you can use the ListBox.SelectedItem</remarks>
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Label1.Text = DirectCast(ListBox1.SelectedItem, Product).price.ToString("c2")
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 22nd, 2012, 07:42 PM
#3
Hyperactive Member
Re: Reading from text file and storing into an array
.paul.'s code should works fine..
I'm still on the path of learning.... 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|