VB .NET Help

Oct 17, 2003
212
0
0
Ok I have a CSV .txt file that looks like this:

Moore,Jim,(111)111-1111
Hathoway,Lawerence,(222)222-2222
DeAntonio,Chris,(333)-333-3333


I read each line of the file and want to store the names in an array called memberData and the phone #'s in an array called phoneData. To my exhausation I can not get this to work, below is what I have so far:

If IO.File.Exists(fileName) Then
sr = IO.File.OpenText(fileName)
Else
Message = "Either no file has yet been created or the file "
Message &= fileName & " is not where expected."
MsgBox(Message, , "File Not Found")
End If

Do While (sr.Peek() <> -1)
line = sr.ReadLine
memberData = line.Split(","c)
phoneData = line.Split(","c)

For i = 0 To memberData.GetUpperBound(0)

lstNames.Items.Add(memberData(i))

Next
Loop

sr.Close()
 

tkdkid

Senior member
Oct 13, 2000
956
0
0
Wouldn't that make memberData and phoneData the same thing?

Maybe try this:
dim tempStr as string()
tempstr = line.split(","c)
memberData = tempstr(0) & "," & tempstr(1)
phoneData = tempstr(2)

Also, either I'm not understanding what you're trying to do, or you've got some pretty weird stuff going on there. You need to think through what you want the program to do exactly.
 
Oct 17, 2003
212
0
0
I tried this, but still get an object reference not set to an instance of an object:

Do While (sr.Peek() <> -1)
line = sr.ReadLine
temp = line.Split(","c)

For i = 0 To temp.GetUpperBound(0)
If temp(i).StartsWith("(") Then
phoneData(counter) = temp(i)
counter += 1
End If
lstNames.Items.Add(phoneData(i))
Next
Loop
 

HJB417

Senior member
Dec 31, 2000
763
0
0
sorry it's in c# but this should do what you want. If the csv gets a little bit more complicated, you'll probably want to use the jet engine w/ ado.net to open the file and properly retrieve the data.

System.Collections.ArrayList memberData = new System.Collections.ArrayList();
System.Collections.ArrayList phoneData = new System.Collections.ArrayList();
string path = @"C:\foo.txt";
System.IO.TextReader file = System.IO.File.OpenText(path);
try
{
while(file.Peek() != -1)
{
string line = file.ReadLine();
//get the values in an array
string[] values = line.Split(',');
//add the name to 'memberData' as "FirstName LastName".
memberData.Add(values[1] + " " + values[0]);
//add the phone number
phoneData.Add(values[2]);
}
}
finally
{
//close the file
file.Close();
}
 
Oct 17, 2003
212
0
0
This is where I am stuck now:

For i = 0 To 4
Do While (sr.Peek() <> -1)
line = sr.ReadLine
temp = line.Split(","c)
ReDim Preserve memberData(i)
memberData(i) = temp(0) & "," & temp(1)
Loop
Next

lstNames.Items.Add(memberData(0))
sr.Close()

I am trying to store each name in its seperate array entry instead of having all the names stored in one array location. The code above just stores the entry in the text file, everything keeps getting overwritten.
 

gsiener

Senior member
Jun 14, 2000
436
0
0
Much simpler would be to do a split on the linebreak (vbcrlf) to get each element, then do another split on comma. Then loop through like HJB417 did to pull out the right elements.