|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
New student stuck with .CSVs and ArraysI'm currently trying to read a .CSV file and get all the data into an array so I can work with it in the program. Here is what I currently have. Private Sub IntializeData() Dim AL As New ArrayList Dim sr As IO.StreamReader 'first, let's open the file sr = IO.File.OpenText("csvstocks.txt") Dim Entry As String Do Entry = sr.ReadLine If Not Entry Is Nothing Then Dim B Dim data() As String = Split(Entry, ",") B.stock = data(0) B.numShares = data(1) B.datePurchased = data(2) B.purchasePrice = data(3) B.currentPrice = data(4) AL.Add(B) End If Loop Until Entry = Nothing End Sub I got most of this code from another post on the board, what it does is read the first line of the .CSV into an array, which is perfect, but then it crashes. How do I get all lines into the array? And then when I do get that part, can I add stuff together by doing. B.currentPrice(1) + B.currentPrice(2) Thanks in advance. "RallyDSM" <ebennet***@gmail.com> wrote in news:1163439539.625663.94300 @m73g2000cwd.googlegroups.com:> I'm currently trying to read a .CSV file and get all the data into an Take a look at FileHelpers (do a google search) - it's an open source > array so I can work with it in the program. project on SourceGear. It's a text import library. RallyDSM wrote:
> What do you mean by "it crashes"? Are you getting an exception? What> I got most of this code from another post on the board, what it does is > read the first line of the .CSV into an array, which is perfect, but > then it crashes. > is the exception? Chris RallyDSM wrote:
Show quoteHide quote > I looked again and I noticed that you are not specifying the type of> Private Sub IntializeData() > > Dim AL As New ArrayList > > Dim sr As IO.StreamReader > > 'first, let's open the file > sr = IO.File.OpenText("csvstocks.txt") > > Dim Entry As String > > Do > Entry = sr.ReadLine > > If Not Entry Is Nothing Then > > Dim B > > Dim data() As String = Split(Entry, ",") > > B.stock = data(0) > B.numShares = data(1) > B.datePurchased = data(2) > B.purchasePrice = data(3) > B.currentPrice = data(4) > > AL.Add(B) > End If > > Loop Until Entry = Nothing > > End Sub > object for B. What is B? Also, do yourself a favor and put Option Strict On at the top of your code or enable it in your project. Hi... if you are a student I'm going to suggest (unlike the other response)
that you don't immediately go download some library somewhere. Anybody can cut and paste that doesn't make one a software developer. My advice is to spend a little more time (it's painful but necessary) to solve the problem. And here is the advice I give to every student (customized for your particular example), "if you need to write a program to read a CSV file don't write a program to read a CSV file." Break it all down into parts that work and then put the parts together. So when you take out the Split() and AL.Add() stuff could you reliably open the CSV file and read each line? If it can't do that then the rest doesn't matter and all the extra code is getting in your way. When it can do that proceed. What is Dim B? In answer to your second question, "No" :-) You could add numbers together if they were numbers but you have them as text. And they wouldn't be accessed through "B" which won't exist after you exit the Do loop. You're putting all the data into AL remember. Again I'd suggest you create the StreamReader (an added feature would be to check if the file actually exists first) and simply read through the thing. Report a count of the number of lines or output the text as you read it to confirm that you've accomplished Step 1. Then go on to Step 2. Show quoteHide quote "RallyDSM" <ebennet***@gmail.com> wrote in message news:1163439539.625663.94300@m73g2000cwd.googlegroups.com... > Hello, > > I'm currently trying to read a .CSV file and get all the data into an > array so I can work with it in the program. > > Here is what I currently have. > > Private Sub IntializeData() > > Dim AL As New ArrayList > > Dim sr As IO.StreamReader > > 'first, let's open the file > sr = IO.File.OpenText("csvstocks.txt") > > Dim Entry As String > > Do > Entry = sr.ReadLine > > If Not Entry Is Nothing Then > > Dim B > > Dim data() As String = Split(Entry, ",") > > B.stock = data(0) > B.numShares = data(1) > B.datePurchased = data(2) > B.purchasePrice = data(3) > B.currentPrice = data(4) > > AL.Add(B) > End If > > Loop Until Entry = Nothing > > End Sub > > I got most of this code from another post on the board, what it does is > read the first line of the .CSV into an array, which is perfect, but > then it crashes. > > How do I get all lines into the array? And then when I do get that > part, can I add stuff together by doing. > > B.currentPrice(1) + B.currentPrice(2) > > Thanks in advance. > Hi there
Tom let me to disagree with some part of your point of view, I really belive that is very important to use some time to research and create your own libraries to learn about how the things work, in fact while developing the FileHelpers I learn a lot of things and I´m very happy with it and them worth all the effort. But in the other hand some times the users only need a fast and good way to do things and for this case the FileHelpers are perfect. I must to said that the CSV format has a lot of exceptions and things that are hard to implement at least for me, here you can read a post of Leon Bambrick about the CSV parsing and all the gotchas around it. http://secretgeek.net/csv_trouble.asp Cheers Marcos http://www.filehelpers.com Show quoteHide quote On 13 nov, 15:46, "Tom Leylan" <g...@iamtiredofspam.com> wrote: > Hi... if you are a student I'm going to suggest (unlike the other response) > that you don't immediately go download some library somewhere. Anybody can > cut and paste that doesn't make one a software developer. My advice is to > spend a little more time (it's painful but necessary) to solve the problem. > > And here is the advice I give to every student (customized for your > particular example), "if you need to write a program to read a CSV file > don't write a program to read a CSV file." Break it all down into parts > that work and then put the parts together. > > So when you take out the Split() and AL.Add() stuff could you reliably open > the CSV file and read each line? If it can't do that then the rest doesn't > matter and all the extra code is getting in your way. When it can do that > proceed. > > What is Dim B? > > In answer to your second question, "No" :-) You could add numbers together > if they were numbers but you have them as text. And they wouldn't be > accessed through "B" which won't exist after you exit the Do loop. You're > putting all the data into AL remember. > > Again I'd suggest you create the StreamReader (an added feature would be to > check if the file actually exists first) and simply read through the thing. > Report a count of the number of lines or output the text as you read it to > confirm that you've accomplished Step 1. Then go on to Step 2. > > "RallyDSM" <ebennet***@gmail.com> wrote in messagenews:1163439539.625663.94***@m73g2000cwd.googlegroups.com... > > > > > Hello, > > > I'm currently trying to read a .CSV file and get all the data into an > > array so I can work with it in the program. > > > Here is what I currently have. > > > Private Sub IntializeData() > > > Dim AL As New ArrayList > > > Dim sr As IO.StreamReader > > > 'first, let's open the file > > sr = IO.File.OpenText("csvstocks.txt") > > > Dim Entry As String > > > Do > > Entry = sr.ReadLine > > > If Not Entry Is Nothing Then > > > Dim B > > > Dim data() As String = Split(Entry, ",") > > > B.stock = data(0) > > B.numShares = data(1) > > B.datePurchased = data(2) > > B.purchasePrice = data(3) > > B.currentPrice = data(4) > > > AL.Add(B) > > End If > > > Loop Until Entry = Nothing > > > End Sub > > > I got most of this code from another post on the board, what it does is > > read the first line of the .CSV into an array, which is perfect, but > > then it crashes. > > > How do I get all lines into the array? And then when I do get that > > part, can I add stuff together by doing. > > > B.currentPrice(1) + B.currentPrice(2) > > > Thanks in advance.- Ocultar texto de la cita -- Mostrar texto de la cita - Hi Marcos:
Disagreement is fine but I believe you're missing some of the subtleties. Notice the title contains "new student" so while fast and good might be perfect I'll guess "I downloaded FileHelpers" might get him a "C" for effort it probably won't yield an "A". I can pretty much guarantee he won't get a passing grade on the final unless he has an Internet connection handy. The "goal" of the assignment is (I'm certain) not to teach someone how to read CSV files into arrays. If that is the goal he can send me $25 and I'll write the code for him. The goal is to learn how to declare variables, write DO loops, use the StreamReader, etc. He's learning the basics so he should learn to parse a file. Perhaps then he could write his own FileHelpers one day or if nothing else be qualified for a job as a software developer. Tom "MarcosMeli" <marcosm***@gmail.com> wrote in message Tom let me to disagree with some part of your point of view, I reallynews:1164165390.690633.40240@m73g2000cwd.googlegroups.com... Hi there belive that is very important to use some time to research and create your own libraries to learn about how the things work, in fact while developing the FileHelpers I learn a lot of things and I´m very happy with it and them worth all the effort. But in the other hand some times the users only need a fast and good way to do things and for this case the FileHelpers are perfect. I must to said that the CSV format has a lot of exceptions and things that are hard to implement at least for me, here you can read a post of Leon Bambrick about the CSV parsing and all the gotchas around it. http://secretgeek.net/csv_trouble.asp Cheers Marcos http://www.filehelpers.com Show quoteHide quote On 13 nov, 15:46, "Tom Leylan" <g...@iamtiredofspam.com> wrote: > Hi... if you are a student I'm going to suggest (unlike the other > response) > that you don't immediately go download some library somewhere. Anybody > can > cut and paste that doesn't make one a software developer. My advice is to > spend a little more time (it's painful but necessary) to solve the > problem. > > And here is the advice I give to every student (customized for your > particular example), "if you need to write a program to read a CSV file > don't write a program to read a CSV file." Break it all down into parts > that work and then put the parts together. > > So when you take out the Split() and AL.Add() stuff could you reliably > open > the CSV file and read each line? If it can't do that then the rest > doesn't > matter and all the extra code is getting in your way. When it can do that > proceed. > > What is Dim B? > > In answer to your second question, "No" :-) You could add numbers > together > if they were numbers but you have them as text. And they wouldn't be > accessed through "B" which won't exist after you exit the Do loop. You're > putting all the data into AL remember. > > Again I'd suggest you create the StreamReader (an added feature would be > to > check if the file actually exists first) and simply read through the > thing. > Report a count of the number of lines or output the text as you read it to > confirm that you've accomplished Step 1. Then go on to Step 2. > > "RallyDSM" <ebennet***@gmail.com> wrote in > messagenews:1163439539.625663.94***@m73g2000cwd.googlegroups.com... > > > > > Hello, > > > I'm currently trying to read a .CSV file and get all the data into an > > array so I can work with it in the program. > > > Here is what I currently have. > > > Private Sub IntializeData() > > > Dim AL As New ArrayList > > > Dim sr As IO.StreamReader > > > 'first, let's open the file > > sr = IO.File.OpenText("csvstocks.txt") > > > Dim Entry As String > > > Do > > Entry = sr.ReadLine > > > If Not Entry Is Nothing Then > > > Dim B > > > Dim data() As String = Split(Entry, ",") > > > B.stock = data(0) > > B.numShares = data(1) > > B.datePurchased = data(2) > > B.purchasePrice = data(3) > > B.currentPrice = data(4) > > > AL.Add(B) > > End If > > > Loop Until Entry = Nothing > > > End Sub > > > I got most of this code from another post on the board, what it does is > > read the first line of the .CSV into an array, which is perfect, but > > then it crashes. > > > How do I get all lines into the array? And then when I do get that > > part, can I add stuff together by doing. > > > B.currentPrice(1) + B.currentPrice(2) > > > Thanks in advance.- Ocultar texto de la cita -- Mostrar texto de la > > cita - Rally,
In addition to the others Put On Option Strict. B is obvious a structure or a class. As it is a class than you can declare that dirty done as Public Class B public stock as string public etc as string public sub New (givenstock as string, givenetc as whatever) stock = givenstock etc = given etc end sub End class Have a look in the documentation how to do it nice using properties. Now you can do in your program Dim myB as B(stock, etc0 AL.Add(myB). However easier with CSV files is the OleDB method which gives direct a datatable. http://www.vb-tips.com/dbpages.aspx?ID=1b644f6b-aa01-49f6-bc1f-212f9e0de193 I hope this helps, Cor Show quoteHide quote "RallyDSM" <ebennet***@gmail.com> schreef in bericht news:1163439539.625663.94300@m73g2000cwd.googlegroups.com... > Hello, > > I'm currently trying to read a .CSV file and get all the data into an > array so I can work with it in the program. > > Here is what I currently have. > > Private Sub IntializeData() > > Dim AL As New ArrayList > > Dim sr As IO.StreamReader > > 'first, let's open the file > sr = IO.File.OpenText("csvstocks.txt") > > Dim Entry As String > > Do > Entry = sr.ReadLine > > If Not Entry Is Nothing Then > > Dim B > > Dim data() As String = Split(Entry, ",") > > B.stock = data(0) > B.numShares = data(1) > B.datePurchased = data(2) > B.purchasePrice = data(3) > B.currentPrice = data(4) > > AL.Add(B) > End If > > Loop Until Entry = Nothing > > End Sub > > I got most of this code from another post on the board, what it does is > read the first line of the .CSV into an array, which is perfect, but > then it crashes. > > How do I get all lines into the array? And then when I do get that > part, can I add stuff together by doing. > > B.currentPrice(1) + B.currentPrice(2) > > Thanks in advance. >
A Question on Arrays.
Insert COM object that has been separated from its underlying RCW... Debugger question Crystal Reports Logon Problem Don't see application events question about variables and subprocedure Systemwide-HotKey Problem calling multiple web services add new line in messagebox text - not using VbCrLf - how? |
|||||||||||||||||||||||