4 Replies - 8713 Views - Last Post: 24 May 2010 - 05:45 AM Rate Topic: -----

#1 jwrmo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 19-November 08

Structure statement using module level array to read file

Posted 27 November 2008 - 08:41 AM

I am having problems with this code. Can anyone help me. Here are the instructions on what im supposed to do.

Instructins:

Define a structure named product, The structure should contain 2 variables one as a string and the other as a decimal. The decimal is used to store the price
and the string is used to store the item number.

Declare a module level array that contains 5 product structure variables

Code the main forms load event procedure so that it reads the item number and prices from the itemInfo.txt file
The procedure should store the item numbers and prices in the module level array. It also should add the item numbers
to the list box.

When the user selects an item in the list box the items price should appear in the xPriceLabel. code the
appropriate procedure.

The code I have is as follows



' Project name:	 Glovers Project
' Project purpose:  The project displays the price of an item.
' Created/revised:  <your name> on <current date>

Option Explicit On
Option Strict On
Public Class MainForm
	Structure product
		Public itemnumber As String
		Public itemprice As Decimal
	End Structure

	Private Sub xExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xExitButton.Click
		Me.Close()
	End Sub
	' declares the variable products as the module level array
	' based on the structure
	' there are 5 records in the text file
	Dim products(4) As product
	Private Sub MainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

		Dim file As String = "itemInfo.txt"
		Dim itemnumber As String
		Dim itemprice As String
		Dim newLineIndex As Integer
		Dim productIndex As Integer

		If My.Computer.FileSystem.FileExists(file) Then
			'load text file into this string
			itemnumber = My.Computer.FileSystem.ReadAllText(file)
			'find first newline in string
			newLineIndex = itemnumber.IndexOf(ControlChars.NewLine)
			Do Until newLineIndex = -1
				products(0).itemnumber = itemnumber.Substring(productIndex, newLineIndex)
				Me.xNumbersListBox.Items.Add(products(0).itemnumber)
				itemprice = (itemnumber.Substring(productIndex, newLineIndex - productIndex))
				Me.xNumbersListBox.Items.Add(itemnumber)
				productIndex = newLineIndex + 2
				newLineIndex = itemnumber.IndexOf(ControlChars.NewLine, newLineIndex)

			Loop

		End If

	End Sub

	Private Sub xNumbersListBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles xNumbersListBox.Enter

	End Sub

	Private Sub xPriceLabel_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles xPriceLabel.Enter
	End Sub
End Class



Seems the problem I am having is this line is returning a value less than 0. I dont know how to fix it.
itemprice = (itemnumber.Substring(productIndex, newLineIndex - productIndex))



Is This A Good Question/Topic? 0
  • +

Replies To: Structure statement using module level array to read file

#2 magicmonkey  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: Structure statement using module level array to read file

Posted 27 November 2008 - 09:15 AM

You are making things a bit more complex then they have to. To simplify it you can look at using a IO.StreamReader to open the file and read one line at a time from the file and parse each line into a new item. Or if you want to read the entire file into a big string then use a IO.StringReader on the string to grab each line from the string in a loop. It will make things a bit clean then tracking the current index position in the string.

I am assuming that the text file is formatted with the itemnumber and price seperated by a comma on each line

10234, 1.75
23431, 2.85

*I never tested this code

        Dim file As String = "itemInfo.txt"
        Dim filedata As String
        Dim linedata As String
        Dim linevalues() As String
        Dim productIndex As Integer

        If My.Computer.FileSystem.FileExists(file) Then
            'load text file into this string
            filedata = My.Computer.FileSystem.ReadAllText(file)
            'Read and process each line from the string
            Using sr As New IO.StringReader(filedata)
                linedata = sr.ReadLine
                Do Until linedata Is Nothing
                    linevalues = linedata.Split(",")
                    products(productIndex).itemnumber = linevalues(0)
                    products(productIndex).itemprice = Decimal.Parse(linevalues(1))
                    Me.xNumbersListBox.Items.Add(products(productIndex).itemnumber)
                    linedata = sr.ReadLine
                    productIndex += 1
                Loop
            End Using
        End If


Was This Post Helpful? 0
  • +
  • -

#3 jwrmo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 19-November 08

Re: Structure statement using module level array to read file

Posted 27 November 2008 - 09:53 AM

View Postmagicmonkey, on 27 Nov, 2008 - 08:15 AM, said:

You are making things a bit more complex then they have to. To simplify it you can look at using a IO.StreamReader to open the file and read one line at a time from the file and parse each line into a new item. Or if you want to read the entire file into a big string then use a IO.StringReader on the string to grab each line from the string in a loop. It will make things a bit clean then tracking the current index position in the string.

I am assuming that the text file is formatted with the itemnumber and price seperated by a comma on each line

10234, 1.75
23431, 2.85

*I never tested this code

        Dim file As String = "itemInfo.txt"
        Dim filedata As String
        Dim linedata As String
        Dim linevalues() As String
        Dim productIndex As Integer

        If My.Computer.FileSystem.FileExists(file) Then
            'load text file into this string
            filedata = My.Computer.FileSystem.ReadAllText(file)
            'Read and process each line from the string
            Using sr As New IO.StringReader(filedata)
                linedata = sr.ReadLine
                Do Until linedata Is Nothing
                    linevalues = linedata.Split(",")
                    products(productIndex).itemnumber = linevalues(0)
                    products(productIndex).itemprice = Decimal.Parse(linevalues(1))
                    Me.xNumbersListBox.Items.Add(products(productIndex).itemnumber)
                    linedata = sr.ReadLine
                    productIndex += 1
                Loop
            End Using
        End If


The Text file looks like this.

12AVX
5
23ABC
8.97
23TWT
4.69
34ZAB
12.5
91BAN
34.67

View Postmagicmonkey, on 27 Nov, 2008 - 08:15 AM, said:

You are making things a bit more complex then they have to. To simplify it you can look at using a IO.StreamReader to open the file and read one line at a time from the file and parse each line into a new item. Or if you want to read the entire file into a big string then use a IO.StringReader on the string to grab each line from the string in a loop. It will make things a bit clean then tracking the current index position in the string.

I am assuming that the text file is formatted with the itemnumber and price seperated by a comma on each line

10234, 1.75
23431, 2.85

*I never tested this code

        Dim file As String = "itemInfo.txt"
        Dim filedata As String
        Dim linedata As String
        Dim linevalues() As String
        Dim productIndex As Integer

        If My.Computer.FileSystem.FileExists(file) Then
            'load text file into this string
            filedata = My.Computer.FileSystem.ReadAllText(file)
            'Read and process each line from the string
            Using sr As New IO.StringReader(filedata)
                linedata = sr.ReadLine
                Do Until linedata Is Nothing
                    linevalues = linedata.Split(",")
                    products(productIndex).itemnumber = linevalues(0)
                    products(productIndex).itemprice = Decimal.Parse(linevalues(1))
                    Me.xNumbersListBox.Items.Add(products(productIndex).itemnumber)
                    linedata = sr.ReadLine
                    productIndex += 1
                Loop
            End Using
        End If


As per the instructions above the code must use a structure and a module level array.
Was This Post Helpful? 0
  • +
  • -

#4 jwrmo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 19-November 08

Re: Structure statement using module level array to read file

Posted 28 November 2008 - 11:52 PM

I really need help with this my code now looks like this, I just cant get it to work, can anyone please help Ive worked hard and long on this one and cant see why it will not function properly.My code now looks like this.
' Project name:	 Glovers Project
' Project purpose:  The project displays the price of an item.
' Created/revised:  <your name> on <current date>

Option Explicit On
Option Strict On

Public Class MainForm
	Structure product
		Public itemnumber As String
		Public itemprice As Decimal
	End Structure
	Dim products(4) As product

	Private Sub xExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xExitButton.Click
		Me.Close()
	End Sub


	Private Sub MainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

		Dim file As String = "itemInfo.txt"
		Dim itemnumber As String
		Dim itemprice As String
		Dim newLineIndex As Integer
		Dim productIndex As Integer

		If My.Computer.FileSystem.FileExists(file) Then
			'load text file into this string  
			itemnumber = My.Computer.FileSystem.ReadAllText(file)
			'find first newline in string  
			newLineIndex = itemnumber.IndexOf(ControlChars.NewLine)
			Do Until newLineIndex <= 0
				products(0).itemnumber = itemnumber.Substring(productIndex, newLineIndex)
				Me.xNumbersListBox.Items.Add(products(0).itemnumber)
				itemprice = (itemnumber.Substring(productIndex, newLineIndex - productIndex))
				Me.xNumbersListBox.Items.Add(itemnumber)
				productIndex = newLineIndex + 2
				newLineIndex = itemnumber.IndexOf(ControlChars.NewLine, newLineIndex)

			Loop

		End If

	End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#5 Guest_Borg*


Reputation:

Re: Structure statement using module level array to read file

Posted 24 May 2010 - 05:45 AM

Well this is probably to old for the guy that originally posted, but........

Here is a solution that works.


' Name:         Glovers Project
' Purpose:      Displays the price of an item
' Programmer:   Name on Date
Option Explicit On
Option Strict On
Option Infer Off

Public Class frmMain

    Structure Product
        Public ItemNum As String
        Public Price As Double
    End Structure

    Private ProdList(4) As Product

    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 System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim Start, Delim, Count As Integer
        Dim ItemNum, Price, Filetext As String

        If My.Computer.FileSystem.FileExists("ItemInfo.txt") Then
            Filetext = My.Computer.FileSystem.ReadAllText("ItemInfo.txt")
        Else
            MessageBox.Show("No such file.", "File Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If

        Delim = Filetext.IndexOf(vbCrLf)
        Do Until Delim = -1
            ItemNum = Filetext.Substring(Start, Delim - Start)
            Start = Delim + 2
            Delim = Filetext.IndexOf(vbCrLf, Start)
            Price = Filetext.Substring(Start, Delim - Start)
            Start = Delim + 2
            Delim = Filetext.IndexOf(vbCrLf, Start)
            ProdList(Count).ItemNum = ItemNum
            ProdList(Count).Price = Convert.ToDecimal(Price)
            Count = Count + 1
            lstNumbers.Items.Add(ItemNum)
        Loop
    End Sub

    Private Sub lstNumbers_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstNumbers.SelectedIndexChanged
        lblPrice.Text = ProdList(lstNumbers.SelectedIndex).Price.ToString("c")
    End Sub
End Class


Was This Post Helpful? 0

Page 1 of 1