Results 1 to 17 of 17

Thread: Better way of Writing this code? (anyone???)

Threaded View

  1. #1

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Resolved Better way of Writing this code? (anyone???)

    Hi there

    I have a function which takes in the data to be written into a file which is specified and it will add it in terms of column

    so

    Time
    1
    2
    3
    4

    Then i add another column

    Time,Monkeys
    1,120
    2,200
    3,201
    4,202

    I am using this code...

    is there a better way of doing this because I find if i overuse this function, it will throw up an error about index out of range???

    VB Code:
    1. Private Function LoadCSV(ByRef arrData As ArrayList, ByRef FileToPutIn As String)
    2.         'We want to create another file, so that while it reads one, it dumps the data into the other.
    3.         Dim sReader As StreamReader
    4.         Dim sWriter As StreamWriter
    5.         Dim arrTemp As Array
    6.         Dim al As New ArrayList
    7.         Dim i As Integer = 0
    8.         Dim strtemp As String
    9.         Dim fi As New IO.FileInfo(FileToPutIn)
    10.         al.Clear()
    11.  
    12.         If fi.Length > 1 Then
    13.             sReader = IO.File.OpenText(FileToPutIn)
    14.             Do While sReader.Peek <> -1
    15.                 al.Add(sReader.ReadLine)
    16.             Loop
    17.             sReader.Close()
    18.             IO.File.Delete(FileToPutIn)
    19.             sWriter = IO.File.CreateText(FileToPutIn)
    20.             For i = 0 To al.Count - 1
    21.                 sWriter.WriteLine(al(i) & "," & arrData(i))
    22.             Next
    23.             sWriter.Close()
    24.         Else
    25.             sWriter = IO.File.CreateText(FileToPutIn)
    26.             For i = 0 To arrData.Count - 1
    27.                 sWriter.WriteLine(arrData(i))
    28.             Next
    29.             sWriter.Close()
    30.         End If
    31.     End Function
    Last edited by dinosaur_uk; Apr 1st, 2005 at 06:41 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width