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:
Private Function LoadCSV(ByRef arrData As ArrayList, ByRef FileToPutIn As String)
'We want to create another file, so that while it reads one, it dumps the data into the other.
Dim sReader As StreamReader
Dim sWriter As StreamWriter
Dim arrTemp As Array
Dim al As New ArrayList
Dim i As Integer = 0
Dim strtemp As String
Dim fi As New IO.FileInfo(FileToPutIn)
al.Clear()
If fi.Length > 1 Then
sReader = IO.File.OpenText(FileToPutIn)
Do While sReader.Peek <> -1
al.Add(sReader.ReadLine)
Loop
sReader.Close()
IO.File.Delete(FileToPutIn)
sWriter = IO.File.CreateText(FileToPutIn)
For i = 0 To al.Count - 1
sWriter.WriteLine(al(i) & "," & arrData(i))
Next
sWriter.Close()
Else
sWriter = IO.File.CreateText(FileToPutIn)
For i = 0 To arrData.Count - 1
sWriter.WriteLine(arrData(i))
Next
sWriter.Close()
End If
End Function
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."
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 :(
Re: Better way of Writing this code? (anyone???)
The where do you get the error message?
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.
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....
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
Re: Better way of Writing this code? (anyone???)
Hi,
I'd use a database rather than a text file.
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?
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...
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.
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?
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.
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. :)
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...
Re: Better way of Writing this code? (anyone???)
No probs, happy to help.
:wave:
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...