Results 1 to 17 of 17

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

  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.

  2. #2
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

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

    Hi,

    I would have assumed that you are getting that error message in connection with

    Dim arrTemp As Array

    but I can't see where you tried to put that object to use. Where do you get the error message?


    Quote from MSDN Help

    "The Array class is the base class for language implementations that support arrays. However, only the system and compilers can derive explicitly from the Array class. Users should use the array constructs provided by the language."
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  3. #3

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

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

    No i am not using the arrTemp...

    is there a way I can open the file tell the pointer to go to the end of each line and drop a value there?

    this problem has been killing me for monthsnow

  4. #4
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

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

    The where do you get the error message?
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  5. #5
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

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

    Dinosaur:

    You pass a parameter to this function: ByRef arrData As ArrayList

    Please show us the original definition of the parameter that gets passed into the function ... somewhere you call: LoadCSV(??????, filestring)

    What is the full definition for the ?????? parameter? If it isn't sized at least as big as the "al" array, it will indeed fail. One thing you can do is pop up a TEST message box in your function to display the item count for al and for arrData. That will show up any discrepancies right away.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  6. #6

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

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

    Quote Originally Posted by taxes
    The where do you get the error message?
    I understand now......i figured out why i was getting the message..

    Basically my sub was passing arrData with different sizes to the LoadCSV function so it was trying to write to a non-existant column....

  7. #7

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

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

    Quote Originally Posted by Webtest
    Dinosaur:

    You pass a parameter to this function: ByRef arrData As ArrayList

    Please show us the original definition of the parameter that gets passed into the function ... somewhere you call: LoadCSV(??????, filestring)

    What is the full definition for the ?????? parameter? If it isn't sized at least as big as the "al" array, it will indeed fail. One thing you can do is pop up a TEST message box in your function to display the item count for al and for arrData. That will show up any discrepancies right away.
    The original definition of arrData is just an arraylist which holds single values. Yeah ....you are right,...i found that one of the arrData was indeed bigger than the al....

    Do you know of any better way of doing what i am trying to do?

    Cheers

  8. #8
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

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

    Hi,

    I'd use a database rather than a text file.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  9. #9

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

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

    Well my option is to use csv files or to use access,

    is access good enough to use? how hard is it?

  10. #10

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

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

    The thing is that I need to create a program which has minimal reliance on other programs....

    I am using the program to extract data from a file format and to put it into a csv file...

  11. #11
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

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

    Hi,

    Access would be easy to use for the purpose you require. It sounds like you will need a full tutorial on accessing databases. There are walkthroughs in MSDN Help and several good tutorials you can find using google. Try it and post any problems you get.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  12. #12

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

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

    Cheers,

    I have installed MySQL and need to see how good it is?

    What do you think of MySQL as a starting db to learn?

  13. #13
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

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

    Hi,

    Never tried it. Access is pefectly OK for all my purposes. The coding is not much different.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  14. #14
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

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

    Hello

    I too have started using MySQL with VB.net, and I found some interesting articles at this site. Mike hillyer is writing a series of articles that are based around creating a sample application using VB.net and mysql as the database.

    Hope this helps.
    Last edited by gep13; Feb 17th, 2005 at 08:03 AM.

  15. #15

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

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

    You are a legend ...i have searching soooo long for a site like this....

    i will be keeping regular contact with you...

  16. #16
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

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

    No probs, happy to help.


  17. #17

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

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

    My girlfriend is in aberdeen at the moment for an interview...she says it is a very very beautiful place...

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