3 Replies - 1176 Views - Last Post: 03 April 2009 - 01:50 PM Rate Topic: -----

#1 Whit3Hax0r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-March 09

Help with File I/O

Post icon  Posted 03 April 2009 - 09:27 AM

I'm trying to create a program where I open a file using a open file dialog and take the information from a text file and output them into various listboxes and textboxes

The first problem I had encountered was at :

With CurrentCustomer
				.EmployeeID = ToInt32(Fields(0))
				.Name = Fields(1)
				.Address = Fields(2)
				.SSN = Fields(3)
				.PayrollDate = Date(4)
				.GrossPay = ToSingle(Fields(5))
				.WithholdTax = ToSingle(Fields(6))
				.FICA = ToSingle(Fields(7))
				.NetPay = ToSingle(Fields(8))
			End With



I have problems with Date(4), saying:
'Date' is a type and cannot be used as an expression

Even after I ignore it, and the program runs(probably not working cuz thats not right)

but on the next line:
.GrossPay = ToSingle(Fields(5))

the next error that generates is:
Index was outside the bounds of the array.

I really need alot of help with this



Class
Option Strict On
Option Explicit On
Imports System.IO
Imports System.Convert

' The structure describing a customer.
Public Structure Customer
	Public EmployeeID As Integer
	Public Name As String
	Public Address As String
	Public SSN As String
	Public PayrollDate As Date
	Public GrossPay As Double
	Public WithholdTax As Single
	Public FICA As Single
	Public NetPay As Single
End Structure

' The CustomerList class contains members to read and write a customers file into a hidden array. Additional methods
' locate a customer, and add, change and delete a customer.
Public Class CustomerList

	Private CurrentReader As StreamReader
	Private CurrentWriter As StreamWriter

	Private CurrentList(0) As Customer
	Private RecordCount As Integer

	' Read the customer file when an instance of the CustomerList class is created.
	Public Sub New(ByVal argFile As String)

		' Create the StreamReader.
		CurrentReader = New StreamReader(argFile)

		' Call the hidden procedure to read the input file.
		Call ReadCustomers(argFile)

		' Close the StreamReader.
		CurrentReader.Close()
	End Sub

	' The hidden ReadCustomers procedure is used to read the input file. The file name is passed as an argument.
	Private Sub ReadCustomers(ByVal argfile As String)
		Dim CurrentCustomer As Customer
		Dim Fields() As String
		Dim CurrentRecord As String
		Dim DelimiterChars() As Char = {ToChar(",")}

		' Perform the priming read to read the first line (record) in the file.
		CurrentRecord = CurrentReader.ReadLine()

		' Process each record until there are no more records to be processed.
		Do Until CurrentRecord Is Nothing

			' Split the current record using the delimiter character.
			Fields = CurrentRecord.Split(DelimiterChars)

			' Copy the fields to the current record performing type conversion as necessary.
			With CurrentCustomer
				.EmployeeID = ToInt32(Fields(0))
				.Name = Fields(1)
				.Address = Fields(2)
				.SSN = Fields(3)
				.PayrollDate = Date(4)
				.GrossPay = ToSingle(Fields(5))
				.WithholdTax = ToSingle(Fields(6))
				.FICA = ToSingle(Fields(7))
				.NetPay = ToSingle(Fields(8))
			End With

			' Increase the size of the list by one element.
			ReDim Preserve CurrentList(RecordCount)

			' Store the current structure in the list.
			CurrentList(RecordCount) = CurrentCustomer

			' Read the next line and increment the record counter.
			CurrentRecord = CurrentReader.ReadLine()
			RecordCount += 1
		Loop

		' Decrement the record counter because its value is overstated by one.
		RecordCount -= 1
	End Sub

	' Write the array back to a file. The file name is passed as an argument to the method.
	Public Sub WriteCustomers(ByVal argfile As String)

		' Create the StreamWriter.
		CurrentWriter = New StreamWriter(argfile)
		Dim CurrentRecord As Integer

		' The For loop writes each record.
		For CurrentRecord = 0 To RecordCount
			With CurrentList(CurrentRecord)
				CurrentWriter.Write(.EmployeeID)
				CurrentWriter.Write(",")
				CurrentWriter.Write(.Name)
				CurrentWriter.Write(",")
				CurrentWriter.Write(.Address)
				CurrentWriter.Write(",")
				CurrentWriter.Write(.SSN)
				CurrentWriter.Write(",")
				CurrentWriter.Write(.PayrollDate)
				CurrentWriter.Write(",")
				CurrentWriter.Write(.GrossPay.ToString)
				CurrentWriter.Write(",")
				CurrentWriter.Write(.WithholdTax.ToString)
				CurrentWriter.Write(",")
				CurrentWriter.Write(.FICA.ToString)
				CurrentWriter.Write(",")
				CurrentWriter.Write(.NetPay.ToString)
				CurrentWriter.WriteLine()
			End With
		Next

		' Close the file when writing is complete.
		CurrentWriter.Close()
	End Sub

	' Get the largest index of the array.
	Public Function GetLargestIndex() As Integer
		Return CurrentList.GetUpperBound(0)
	End Function

	' Return a Customer structure. The index of the customer is passed as an argument.
	Public Function GetCustomer(ByVal arg As Integer) As Customer
		Return CurrentList(arg)
	End Function

	' Add a new customer. The customer is passed as an argument.
	Public Sub AddCustomer(ByVal arg As Customer)

		' Increment the record counter because there is now one more record.
		RecordCount += 1

		' Increase the array size by one.
		ReDim Preserve CurrentList(RecordCount)

		' Store the customer, passed as an argument, in the
		' array.
		CurrentList(RecordCount) = arg
	End Sub

	' Replace a customer. The index of the customer to replace and the
	' customer are passed as arguments.
	Public Sub ChangeCustomer(ByVal arg As Customer, ByVal argIndex As Integer)
		CurrentList(argIndex) = arg
	End Sub

	' Delete the current customer.
	Public Sub DeleteCustomer(ByVal argIndex As Integer)
		Dim Count As Integer

		' The loop shifts the array elements.
		For Count = argIndex To CurrentList.GetUpperBound(0) - 1
			CurrentList(Count) = CurrentList(Count + 1)
		Next

		' Decrease the array size by one.
		ReDim Preserve CurrentList(CurrentList.GetUpperBound(0) - 1)

		' Decrement the record counter.
		RecordCount -= 1
	End Sub

End Class
 


Main Source
Option Strict On
Option Explicit On
Imports System.IO
Imports System.Convert

Public Class Form1
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim CurrentCustomerList As CustomerList
		Dim result As DialogResult
		ofdMain.Filter = "Text Files(*.txt)|*.txt|All Files(*.*)|*.*"
		result = ofdMain.ShowDialog
		If result = Windows.Forms.DialogResult.OK Then
			CurrentCustomerList = New CustomerList(ofdMain.FileName)
		End If
	End Sub

	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
		Close()
	End Sub
End Class



Is This A Good Question/Topic? 0
  • +

Replies To: Help with File I/O

#2 krum110487  Icon User is offline

  • D.I.C Regular

Reputation: 39
  • View blog
  • Posts: 291
  • Joined: 07-February 09

Re: Help with File I/O

Posted 03 April 2009 - 10:42 AM

Date is the datatype, that would be the same as putting =Integer(4)...that makes no sense.

you want something like "Month(4)" (that may not be exact)

As for the outside of index, that is because it doesn't know what Fields(0) - Fields(8) are... have to declare them.

This post has been edited by krum110487: 03 April 2009 - 10:43 AM

Was This Post Helpful? 0
  • +
  • -

#3 Whit3Hax0r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-March 09

Re: Help with File I/O

Posted 03 April 2009 - 12:36 PM

ok I made some changes and progress
I'm able to out the first line of text from the text file into textboxes, but I need to input the rest of the lines of text. I have a combo box that lists the IDNumber and everytime I click on a different number it should fill in the rest of the textboxes with the corresponding information. how do I do this?
Option Explicit On
Option Strict On

Imports System.IO
Imports System.Convert

Public Class frmMain

	Private CurrentCustomerList As CustomerList
	Private CurrentEmployeeList As CustomerList
	Private CurrentRecord As Integer

	' Open the input file.
	Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
		Dim Result As DialogResult
		ofdMain.Filter = "Text Files(*.txt)|*.txt|All Files(*.*)|*.*"
		Result = ofdMain.ShowDialog

		' If the user clicked OK, then create an instance of the
		' CustomerList class passing the file name to open as an 
		' argument.
		If Result = Windows.Forms.DialogResult.OK Then
			CurrentCustomerList = New CustomerList(ofdMain.FileName)
		End If

		Call DisplayCustomer(0)
	End Sub
	' Display the current record.
	Private Sub DisplayCustomer(ByVal index As Integer)

		' Get the customer from the list and copy the data
		' from the structure members to the text boxes.
		With CurrentCustomerList.GetCustomer(index)
			ComboBox1.Text = CStr(.IDNumber)
			txtName.Text = .Name
			txtAddress.Text = .Address
			txtSSN.Text = .SSN
		End With
		'With CurrentCustomerList.GetCustomer(index)
		'ListBox1.Items.Add(.PayrollDate)
		'ListBox2.Items.Add(.GrossPay)
		'ListBox3.Items.Add(.withholdTax)
		'ListBox4.Items.Add(.FICA)
		'ListBox5.Items.Add(.NetPay)
		'End With

	End Sub

	Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
		Close()
	End Sub
End Class



Option Explicit On
Option Strict On

Imports System.IO
Imports System.Convert

' The structure describing a customer.
Public Structure Customer
	Public IDNumber As Integer
	Public Name As String
	Public Address As String
	Public SSN As String
End Structure
'Public PayrollDate As Date
'Public GrossPay As Single
'Public WithholdTax As Single
'Public FICA As Single
'Public NetPay As Single


' The CustomerList class contains members to read and write
' a customers file into a hidden array. Additional methods 
' locate a customer, and add, change and delete a customer.
Public Class CustomerList

	Private CurrentReader As StreamReader
	Private CurrentWriter As StreamWriter

	Private CurrentList(0) As Customer
	Private RecordCount As Integer

	' Read the customer file when an instance of the CustomerList
	' class is created. The file name is passed as an argument to
	' the constructor.
	Public Sub New(ByVal argFile As String)

		' Create the StreamReader.
		CurrentReader = New StreamReader(argFile)

		' Call the hidden procedure to read the input file.
		Call ReadCustomers(argFile)

		' Close the StreamReader.
		CurrentReader.Close()
	End Sub

	' The hidden ReadCustomers procedure is used to read
	' the input file. The file name is passed as an argument.
	Private Sub ReadCustomers(ByVal argfile As String)
		Dim CurrentCustomer As Customer

		Dim Fields() As String
		Dim CurrentRecord As String
		Dim DelimiterChars() As Char = {ToChar(",")}

		' Perform the priming read to read the first
		' line (record) in the file.
		CurrentRecord = CurrentReader.ReadLine()

		' Process each record until there are no more
		' records to be processed.
		Do Until CurrentRecord Is Nothing

			' Split the current record using the delimiter
			' character.
			Fields = CurrentRecord.Split(DelimiterChars)

			' Copy the fields to the current record performing
			' type conversion as necessary.
			With CurrentCustomer
				.IDNumber = ToInt32(Fields(0))
				.Name = Fields(1)
				.Address = Fields(2)
				.SSN = Fields(3)
			End With
			'.PayrollDate = CDate(Fields(4))
			'.GrossPay = ToSingle(Fields(5))
			'.WithholdTax = ToSingle(Fields(6))
			'.FICA = ToSingle(Fields(7))
			'.NetPay = ToSingle(Fields(8))


			' Increase the size of the list by one element.
			ReDim Preserve CurrentList(RecordCount)

			' Store the current structure in the list.
			CurrentList(RecordCount) = CurrentCustomer

			' Read the next line and increment the record
			' counter.
			CurrentRecord = CurrentReader.ReadLine()
			RecordCount += 1
		Loop

		' Decrement the record counter because its value is
		' overstated by one.
		RecordCount -= 1
	End Sub

	' Write the array back to a file. The file name is passed
	' as an argument to the method.
	Public Sub WriteCustomers(ByVal argfile As String)

		' Create the StreamWriter.
		CurrentWriter = New StreamWriter(argfile)
		Dim CurrentRecord As Integer

		' The For loop writes each record.
		For CurrentRecord = 0 To RecordCount
			With CurrentList(CurrentRecord)
				CurrentWriter.Write(.IDNumber)
				CurrentWriter.Write(",")
				CurrentWriter.Write(.Name)
				CurrentWriter.Write(",")
				CurrentWriter.Write(.Address)
				CurrentWriter.Write(",")
				CurrentWriter.Write(.SSN)
				CurrentWriter.Write(",")
				CurrentWriter.WriteLine()
			End With
		Next

		' Close the file when writing is complete.
		CurrentWriter.Close()
	End Sub

	' Get the largest index of the array. 
	Public Function GetLargestIndex() As Integer
		Return CurrentList.GetUpperBound(0)
	End Function

	' Return a Customer structure. The index of the customer
	' is passed as an argument.
	Public Function GetCustomer(ByVal arg As Integer) As Customer
		Return CurrentList(arg)
	End Function
End Class


Was This Post Helpful? 0
  • +
  • -

#4 moyue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-April 09

Re: Help with File I/O

Posted 03 April 2009 - 01:50 PM

I only know that you can read a whole file with the streamreader,

it's something like this:

	Dim strReader As StreamReader = File.OpenText("woorden.txt")
	Dim line As String
	line = strReader.ReadLine()
		messagebox.show(line)

		while line <> noting then
			line = strReader.ReadLine()
				messagebox.show(line)
		end while

	strReader.Close()



search for the "split" function to split the line in de the parts you want

This post has been edited by moyue: 03 April 2009 - 01:54 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1