VB.Net 2005 Read and Write a Sequential File

Sequential file contains a rolodex (richtext file)

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

36 Replies - 6524 Views - Last Post: 20 August 2008 - 07:10 PM Rate Topic: -----

#1 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

VB.Net 2005 Read and Write a Sequential File

Post icon  Posted 07 August 2008 - 11:59 PM

I have to read and write a sequential file that contains a rolodex (richtext file). I have 6 textboxes for 6 fields lastName, firstName, Address, ContactDate, City, State, and a maskedTextbox for ZipCode, and richtextbox for notes. I am having a problem writing code for the textboxes that suppose to navigate from one record at a time. Here is my code:
I appreciate for your help. This is written in Visual Basic 2005 from the book Programming with Microsoft VB 2005 Object Oriented Approach byy Michael Ekedahl, 2007.
Option Explicit On
Option Strict On

Imports System.Convert
Imports System.IO

' The ContactReader class is used to read the
' sequential file containing the contacts.
Public Class RolodexList

	Private CurrentReader As StreamReader

	' Create an instance of the StreamReader when the
	' class instance is created.
	Public Sub New(ByVal argFile As String)
		CurrentReader = New StreamReader(argFile)
	End Sub

	' Read the contact file.
	Public Function ReadContact() As Rolodex
		Dim CurrentContact As Rolodex
		Dim Fields() As String
		Dim CurrentRecord As String

		' Define the delimiter character.
		Dim DelimiterChars() As Char = {ToChar(",")}

		' Read a line from the file.
		CurrentRecord = CurrentReader.ReadLine()

		' Split the line into the respective fields.
		Fields = CurrentRecord.Split(DelimiterChars)

		' Populate the contact record.
		With CurrentContact
			.lastName = Fields(0)
IndexOutOfRangeException--->	.firstName = Fields(1) <-- My error
			.Address = Fields(2)
			.ContactDate = ToDateTime(Fields(3))
			.City = Fields(4)
			.State = Fields(5)
			.ZipCode = Fields(6)

		End With

		' Return the contact record.
		Return CurrentContact
	End Function

	' Determine whether end of file is true by 
	' trying to examine the next character. End of file
	' is true if there are no more characters.
	Function EndOfFile() As Boolean
		Dim NextCharacter As Integer
		NextCharacter = CurrentReader.Peek()
		If NextCharacter = -1 Then
			Return True
		End If
	End Function

	' Close the file referenced by CurrentReader.
	Public Sub Close()
		CurrentReader.Close()
	End Sub
End Class

' Declare the structure named Contact to store
' the contact information.
Public Structure Rolodex
	Public lastName As String
	Public firstName As String
	Public Address As String
	Public ContactDate As Date
	Public City As String
	Public State As String
	Public ZipCode As String

End Structure

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

Public Class Form1

	Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp

		If My.Computer.Keyboard.NumLock = True Then
			tsslNumLock.Text = "Num"
		Else
			tsslNumLock.Text = ""

		End If

		If My.Computer.Keyboard.CapsLock = True Then
			tsslCapsLock.Text = "Caps"
		Else
			tsslCapsLock.Text = ""

		End If

		If My.Computer.Keyboard.ScrollLock = True Then
			tsslScrollLock.Text = "Scroll"
		Else
			tsslScrollLock.Text = ""

		End If
	End Sub

	
	Private Sub ColorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ColorToolStripMenuItem.Click

		Dim Result As DialogResult

		Result = colorDialog.ShowDialog

		If Result = Windows.Forms.DialogResult.OK Then
			rtbMain.SelectionColor = colorDialog.Color

		End If
	End Sub

	Private Sub FontToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles FontToolStripMenuItem.Click

		Dim result As DialogResult

		result = fontDialog.ShowDialog

		If result = Windows.Forms.DialogResult.OK Then
			rtbMain.SelectionFont = fontDialog.Font

		End If

	End Sub
	Private Sub OpenFile()

		Dim Result As DialogResult

		openDialog.Filter = "Rich Text(*.rtf)|*.rtf" & _
		"Text Files(*.txt)|(*.txt)|*.txt|All Files(*.*)|*.*"
		openDialog.FilterIndex = 2

		Result = openDialog.ShowDialog

		If Result = Windows.Forms.DialogResult.OK Then
			rtbMain.LoadFile(openDialog.FileName, _
			RichTextBoxStreamType.PlainText)

		End If

		tsslFileName.Text = openDialog.FileName


	End Sub
   

	Private Sub SaveFile()

		Dim result As DialogResult

		result = saveDialog.ShowDialog

		If result = Windows.Forms.DialogResult.OK Then
			rtbMain.SaveFile(saveDialog.FileName, _
			RichTextBoxStreamType.RichText)

		End If
	End Sub

	Private Sub NavigateFirst()

		rtbMain.Selectionstart = 0
		rtbMain.SelectionLength = 0

	End Sub

	Private Sub NavigateLast()

		rtbMain.Selectionstart = rtbMain.Text.Length - 1

	End Sub

	Private Sub mnuOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuOpen.Click

		Call OpenFile()


	End Sub

	Private Sub mnuSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSave.Click

		Call SaveFile()

	End Sub

	Private Sub tsFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsFirst.Click

		Call NavigateFirst()

	End Sub

	Private Sub tsLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsLast.Click

		Call NavigateLast()

	End Sub
	Private CurrentRolodexList() As Rolodex
	Private CurrentRecord As Integer = 0
	Private NextRecord As Integer = 0

	Private Sub tsOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsOpen.Click
		Dim CurrentReader As RolodexList
		Dim CurrentContact As Rolodex
		Dim Result As DialogResult
		openDialog.Filter = "Text Files(*.txt)|*.txt|All Files(*.*)|*.*"
		Result = openDialog.ShowDialog
		If Result = Windows.Forms.DialogResult.OK Then

			' Create the instance of the ContactReader class thereby creating
			' the underlying StreamReader
			CurrentReader = New RolodexList(openDialog.FileName)

			' Read and proces until the end of file has been reached.
			Do Until CurrentReader.EndOfFile
				CurrentContact = CurrentReader.ReadContact
				ReDim Preserve CurrentRolodexList(NextRecord)
				CurrentRolodexList(NextRecord) = CurrentContact
				NextRecord += 1
			Loop
			CurrentReader.Close()
		End If


		Call DisplayCurrentRecord(CurrentRecord)


	End Sub

	Private Sub DisplayCurrentRecord(ByVal index As Integer)
		txtfirstName.Text = CurrentRolodexList(index).firstName
		txtlastName.Text = CurrentRolodexList(index).lastName
		txtAddress.Text = CurrentRolodexList(index).Address
		txtContactDate.Text = CurrentRolodexList(index).ContactDate.ToShortDateString
		txtCity.Text = CurrentRolodexList(index).City
		txtState.Text = CurrentRolodexList(index).State
		mtbZipCode.Text = CurrentRolodexList(index).ZipCode

	End Sub
End Class


This post has been edited by bravo659: 08 August 2008 - 12:28 AM


Is This A Good Question/Topic? 0
  • +

Replies To: VB.Net 2005 Read and Write a Sequential File

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: VB.Net 2005 Read and Write a Sequential File

Posted 11 August 2008 - 08:18 AM

The problem is you are assuming that the file is in the correct format, never assume.
Test that is correct before using.
Option Explicit On
Option Strict On

Imports System.Convert
Imports System.IO

' The ContactReader class is used to read the
' sequential file containing the contacts.
Public Class RolodexList

	Private CurrentReader As StreamReader

	' Create an instance of the StreamReader when the
	' class instance is created.
	Public Sub New(ByVal argFile As String)
		CurrentReader = New StreamReader(argFile)
	End Sub

	' Read the contact file.
	Public Function ReadContact() As Rolodex
		Dim CurrentContact As Rolodex
		Dim Fields() As String
		Dim CurrentRecord As String
dim Corrent_Size as integer=7
		' Define the delimiter character.
		Dim DelimiterChars() As Char = {ToChar(",")}

		' Read a line from the file.
		CurrentRecord = CurrentReader.ReadLine()

		' Split the line into the respective fields.
		Fields = CurrentRecord.Split(DelimiterChars)
		if fields.length=Correct_Size Then

		' Populate the contact record.
		With CurrentContact
			.lastName = Fields(0)
	.firstName = Fields(1) <-- My error
			.Address = Fields(2)
			.ContactDate = ToDateTime(Fields(3))
			.City = Fields(4)
			.State = Fields(5)
			.ZipCode = Fields(6)

		End With

		' Return the contact record.
		Return CurrentContact
else
return Nothing
endif
	End Function



You'll need to check for nothing where if called.
Was This Post Helpful? 0
  • +
  • -

#3 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

Re: VB.Net 2005 Read and Write a Sequential File

Posted 12 August 2008 - 07:54 PM

I tried that and didnt work.
Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: VB.Net 2005 Read and Write a Sequential File

Posted 13 August 2008 - 04:05 PM

View Postbravo659, on 13 Aug, 2008 - 03:54 AM, said:

I tried that and didnt work.

Why? Give more information.
Was This Post Helpful? 0
  • +
  • -

#5 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

Re: VB.Net 2005 Read and Write a Sequential File

Posted 13 August 2008 - 04:23 PM

View PostAdamSpeight2008, on 13 Aug, 2008 - 04:05 PM, said:

View Postbravo659, on 13 Aug, 2008 - 03:54 AM, said:

I tried that and didnt work.

Why? Give more information.


What more information you need from the one I gave.
Is doesn't work with the coding and explanations you have given.
I need to send something here that you can understand what it is I am trying to accomplish, if I am allowed. I have an executable file that shows the actual program and I also have he txt files. Can I include into attachments?
Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1632
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: VB.Net 2005 Read and Write a Sequential File

Posted 13 August 2008 - 04:50 PM

What he's asking is how did it not work? Did you get an error? What did it do versus what you need it to do? Saying It didn't work just doesn't give us much to work with :)
Was This Post Helpful? 0
  • +
  • -

#7 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

Re: VB.Net 2005 Read and Write a Sequential File

Posted 13 August 2008 - 04:58 PM

View PostAdamSpeight2008, on 13 Aug, 2008 - 04:05 PM, said:

View Postbravo659, on 13 Aug, 2008 - 03:54 AM, said:

I tried that and didnt work.

Why? Give more information.


Oh sorry, I misinterpreted. I tried what you told me to include in the function return nothing, I did but it still renders the same previous error. "Index was outside the bounds of the array."
That was the previous error that made me seek for help in this forum. :D
Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: VB.Net 2005 Read and Write a Sequential File

Posted 13 August 2008 - 05:09 PM

This what meant by checking for nothing
dim rec as Rolodex=ReadContact
If IsNothing(rec) = True Then
 ' There as problem
Else
 ' Normal processing
End If 


Added debug information to function

Option Explicit On
Option Strict On

Imports System.Convert
Imports System.IO

' The ContactReader class is used to read the
' sequential file containing the contacts.
Public Class RolodexList

	Private CurrentReader As StreamReader

	' Create an instance of the StreamReader when the
	' class instance is created.
	Public Sub New(ByVal argFile As String)
		CurrentReader = New StreamReader(argFile)
	End Sub

	' Read the contact file.
	Public Function ReadContact() As Rolodex
		Dim CurrentContact As Rolodex
		Dim Fields() As String
		Dim CurrentRecord As String
dim Corrent_Size as integer=7
		' Define the delimiter character.
		Dim DelimiterChars() As Char = {ToChar(",")}

		' Read a line from the file.
		CurrentRecord = CurrentReader.ReadLine()

		' Split the line into the respective fields.
		Fields = CurrentRecord.Split(DelimiterChars)
		if fields.length=Correct_Size Then

		' Populate the contact record.
		With CurrentContact
			.lastName = Fields(0)
	.firstName = Fields(1) <-- My error
			.Address = Fields(2)
			.ContactDate = ToDateTime(Fields(3))
			.City = Fields(4)
			.State = Fields(5)
			.ZipCode = Fields(6)

		End With

		' Return the contact record.
		Return CurrentContact
else
' Debug information
debug.print("Number of fields: {0}",fields.length)
return Nothing
endif
	End Function


This post has been edited by AdamSpeight2008: 13 August 2008 - 05:15 PM

Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: VB.Net 2005 Read and Write a Sequential File

Posted 13 August 2008 - 05:18 PM

I also reckon on that there is a blank line in the file,most likely the very last one.

This post has been edited by AdamSpeight2008: 13 August 2008 - 05:19 PM

Was This Post Helpful? 0
  • +
  • -

#10 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

Re: VB.Net 2005 Read and Write a Sequential File

Posted 13 August 2008 - 06:18 PM

View PostAdamSpeight2008, on 13 Aug, 2008 - 05:18 PM, said:

I also reckon on that there is a blank line in the file,most likely the very last one.


Ok, so far there is no error showing, but does not load to the textboxes.
Was This Post Helpful? 0
  • +
  • -

#11 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: VB.Net 2005 Read and Write a Sequential File

Posted 13 August 2008 - 06:29 PM

View Postbravo659, on 14 Aug, 2008 - 02:18 AM, said:

View PostAdamSpeight2008, on 13 Aug, 2008 - 05:18 PM, said:

I also reckon on that there is a blank line in the file,most likely the very last one.


Ok, so far there is no error showing, but does not load to the textboxes.

Debug information is displayed in the output window, it doesn't display a normal error box.

Textboxes? What textboxes? You've only just mentioned textboxes.

This post has been edited by AdamSpeight2008: 13 August 2008 - 06:33 PM

Was This Post Helpful? 0
  • +
  • -

#12 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

Re: VB.Net 2005 Read and Write a Sequential File

Posted 13 August 2008 - 06:40 PM

View PostAdamSpeight2008, on 13 Aug, 2008 - 06:29 PM, said:

View Postbravo659, on 14 Aug, 2008 - 02:18 AM, said:

View PostAdamSpeight2008, on 13 Aug, 2008 - 05:18 PM, said:

I also reckon on that there is a blank line in the file,most likely the very last one.


Ok, so far there is no error showing, but does not load to the textboxes.

Debug information is displayed in the output window, it doesn't display a normal error box.

Textboxes? What textboxes? You've only just mentioned textboxes.


The textboxes where the information is displayed. I have a form with 6 textboxes, one maskedtextbox, and a richtextbox. I mentioned that in my first post and part of my coding as well.
Was This Post Helpful? 0
  • +
  • -

#13 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: VB.Net 2005 Read and Write a Sequential File

Posted 13 August 2008 - 06:54 PM

View Postbravo659, on 14 Aug, 2008 - 02:40 AM, said:

The textboxes where the information is displayed. I have a form with 6 textboxes, one maskedtextbox, and a richtextbox. I mentioned that in my first post and part of my coding as well.

Sorry uncalled for.
Try changing the following section of code.
Do Until CurrentReader.EndOfFile
	CurrentContact = CurrentReader.ReadContact
				ReDim Preserve CurrentRolodexList(NextRecord)
				CurrentRolodexList(NextRecord) = CurrentContact
				NextRecord += 1
			Loop


to
Do Until CurrentReader.EndOfFile
	CurrentContact = CurrentReader.ReadContact
If Isnothing(CurrrentContact)=false then
' Only a valid record
				ReDim Preserve CurrentRolodexList(NextRecord)
				CurrentRolodexList(NextRecord) = CurrentContact
				NextRecord += 1
end if
			Loop


Was This Post Helpful? 0
  • +
  • -

#14 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

Re: VB.Net 2005 Read and Write a Sequential File

Posted 13 August 2008 - 08:21 PM

View PostAdamSpeight2008, on 13 Aug, 2008 - 06:54 PM, said:

View Postbravo659, on 14 Aug, 2008 - 02:40 AM, said:

The textboxes where the information is displayed. I have a form with 6 textboxes, one maskedtextbox, and a richtextbox. I mentioned that in my first post and part of my coding as well.

Sorry uncalled for.
Try changing the following section of code.
Do Until CurrentReader.EndOfFile
	CurrentContact = CurrentReader.ReadContact
				ReDim Preserve CurrentRolodexList(NextRecord)
				CurrentRolodexList(NextRecord) = CurrentContact
				NextRecord += 1
			Loop


to
Do Until CurrentReader.EndOfFile
	CurrentContact = CurrentReader.ReadContact
If Isnothing(CurrrentContact)=false then
' Only a valid record
				ReDim Preserve CurrentRolodexList(NextRecord)
				CurrentRolodexList(NextRecord) = CurrentContact
				NextRecord += 1
end if
			Loop




I really do appreciate your help, but didn't work either and no error messages. I just don't know how is coded. The executable program looks good too. Wish i had a better teacher, because my teacher won't explain anything and told her the difficulty that i am having. Anyway, with that change it still didn't work and no errors rendered. No syntax, logical or runtime error occurred. Now that is weird, I guess we're closer?
:)

This post has been edited by bravo659: 13 August 2008 - 08:23 PM

Was This Post Helpful? 0
  • +
  • -

#15 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: VB.Net 2005 Read and Write a Sequential File

Posted 13 August 2008 - 09:04 PM

View Postbravo659, on 14 Aug, 2008 - 04:21 AM, said:

I really do appreciate your help, but didn't work either and no error messages. I just don't know how is coded. The executable program looks good too. Wish i had a better teacher, because my teacher won't explain anything and told her the difficulty that i am having. Anyway, with that change it still didn't work and no errors rendered. No syntax, logical or runtime error occurred. Now that is weird, I guess we're closer?
:)

Nearly there. you just need to a few more function calls to DisplayCurrentRecord

Add a global variable to the code, which keeps track of which record is current ly displayed, you may have one.
I'll use Current_Record for example purposes.
Dim Current_Record as Integer=0


' Display first record
Current_Record=0
DisplayCurrentRecord(Current_Record)


To move on to next
' First check to see if its not on the last one
If Current_Record<Last_Record Then
Current_Record+=1
DisplayCurrentRecord(Current_Record)
End If


To move on to previous
' First check to see if its not on the last one
If Current_Record>0 Then
Current_Record-=1
DisplayCurrentRecord(Current_Record)
End If



Any problems report back.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3