pm wrote:
<backposted/>
To resize the array, you can either:
a) use Redim Preserve:
'assuming that NumRex has the new line count
Redim Preserve BookArray(0 To NumRex-1)
b) use some collection instead of an array. This way you don't need to
pre-fetch the lines to properly dim the array previouly to actually
filling it:
<aircode>
Dim Books As New List(Of Book)
'In your Load event:
Books.Clear
Do While oRead.Peek <-1
Dim Items() As String = oRead.ReadLine().Split(","c)
Dim NewBook As New Book
With NewBook
.BookId = CInt(Items(0))
.Title = Items(1)
.Author = Items(2)
.Publisher = Items(3)
.Datepublished = Items(4)
End With
Books.Add(NewBook)
Loop
oRead.Close
NumRex = Books.Count
</aircode>
Now, for adding a new record and saving it to the original file, you
can either
a) implement a "save all" functionality, where you'd overwrite the
original file with the current lines taken from the books:
<aircode>
'Create a backup and recover from errors is left as an exercise
Dim Out as New System.IO.StreamWriter(STR_OUTFILE)
For Each B As Book in Books
Dim Line As String = String.Format("{0},{1},{2},{3},{4}", _
B.BookId, B.Title, B.Author, _
B.Publisher, B.Datepublished)
Out.WriteLine(Line)
Next
Out.Close
</aircode>
b) Open the file for appending and write just the new book:
<aircode>
Sub AddBook(NewBook As Book)
Books.Add(NewBook)
NumRex = Books.Count
Dim Out As Suystem.IO.StreamWriter = _
System.IO.File.AppendText(STR_OUTFILE)
Dim Line As String = String.Format("{0},{1},{2},{3},{4}", _
NewBook.BookId, NewBook.Title, _
NewBook.Author, NewBook.Publisher, _
NewBook.Datepublished)
Out.WriteLine(Line)
Out.Close
End Sub
</aircode>
HTH.
Regards,
Branco.
Quote:
hi
>
fairly new to vb.net . I have created a filesystem where a manager
and co workers hve limted access.
>
the manager can read members files. i have used streamreader to get
the details from a text file on my c drive into relevent text boxes eg
name, address by using arrays.
>
what i need to know is , how do i make it possible for the manager to
add a new record on the same page that will be appended to the same
text file on my c drive. and them read bak from it.
>
I also need to know how make my array bigger so say if there where
more records added.
>
any help gratfull- this is driving me mad
>
this is my code.
>
Public Class Form2
Public BookArray As Book() 'Class file
Public NumRex As Integer
Dim Current As Integer
>
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
>
Dim datafile As String = "C:\BooksFile.txt"
Dim oRead As New System.IO.StreamReader(datafile) ' Should
check that the file exists
Dim i As Integer = 0
Dim linein As String
Dim arr As String()
>
'Find out how many lines we've got
While oRead.Peek <-1
linein = oRead.ReadLine()
i = i + 1
'MsgBox(linein & " i = " & i)
End While
NumRex = i - 1
'Redim ItemArray so it'll hold all the items
ReDim BookArray(NumRex)
>
oRead.Close()
oRead = New System.IO.StreamReader(datafile)
>
For i = 0 To CastArray.Length - 1
'Grab a line
linein = oRead.ReadLine()
' Split the line at the commas and feed it into the arr
array
arr = linein.Split(",")
'Now feed the elements of arr into the fields of
ItemArray(i)
BookArray(i) = New Book
BookArray(i).BookId = Int(arr(0))
BookArray(i).Title = arr(1)
BookArray(i).Author = arr(2)
BookArray(i).Publisher = arr(3)
BookArray(i).Datepublished = arr(4)
>
Next i
>
oRead.Close()
>
txtBookId.Text = BookArray(0).BookId
txtTitle.Text = BookArray(0).Title
txtAuthor.Text = BookArray(0).Author
txtPublisher.Text = BookArray(0).Publisher
txtDatePublished.Text = BookArray(0).Datepublished
Current = 0
>
Private Sub btnLastRecord_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles btnLastRecord.Click
>
txtBookId.Text = BookArray(NumRex).BookId
txtTitle.Text = BookArray(NumRex).Title
txtAuthor.Text = BookArray(NumRex).Author
txtPublisher.Text = BookArray(NumRex).Publisher
txtDatePublished.Text = BookArray(NumRex).Datepublished
Current = NumRex
>
Private Sub btnNextRecord_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles btnNextRecord.Click
If Current < NumRex Then
Current = Current + 1
txtBookId.Text = BookArray(Current).BookId
txtTitle.Text = BookArray(Current).Title
txtAuthor.Text = BookArray(Current).Author
txtPublisher.Text = BookArray(Current).Publisher
txtDatePublished.Text = BookArray(Current).Datepublished
>
End If
End Sub