|
-
Jan 27th, 2009, 11:22 AM
#1
Thread Starter
Hyperactive Member
Reading to End of File Causing 1 Extra Date Piece
Hello. I have this piece of code:
vb Code:
Public Sub LoadContact()
With frmMain
If IO.File.Exists(Application.StartupPath & "\Info\Contacts.vis") Then
Dim sr As New IO.StreamReader(Application.StartupPath & "\Info\Contacts.vis")
While (sr.EndOfStream <> True)
Dim sLine As String = sr.ReadLine
Dim sSI() As String = Split(sLine, "|")
sTitle.Add(sSI(0))
sFullName.Add(sSI(1))
sPhone.Add(sSI(2))
sExt.Add(sSI(3))
sFax.Add(sSI(4))
sAddress1.Add(sSI(5))
sAddress2.Add(sSI(6))
sAddress3.Add(sSI(7))
sCity.Add(sSI(8))
sState.Add(sSI(9))
sZipCode.Add(sSI(10))
sEmail.Add(sSI(11))
ContactInfo.Add(sTitle(iCount).ToString & "|" & sFullName(iCount).ToString & "|" & _
sPhone(iCount).ToString & "|" & sExt(iCount).ToString & "|" & _
sFax(iCount).ToString & "|" & sAddress1(iCount).ToString & "|" & _
sAddress2(iCount).ToString & "|" & sAddress3(iCount).ToString & "|" & _
sCity(iCount).ToString & "|" & sState(iCount) & "|" & _
sZipCode(iCount).ToString & "|" & sEmail(iCount).ToString)
.lstContacts.BeginUpdate()
.lstContacts.Items.Clear()
For i As Integer = 0 To UBound(ContactInfo.ToArray)
.lstContacts.Items.Add(sFullName(i))
.lstvwContacts.Items.Add(sTitle(i))
Dim lstitm1, lstitm2, lstitm3 As New ListViewItem.ListViewSubItem
lstitm1.Text = sFullName(i)
lstitm2.Text = sAddress1(i)
lstitm3.Text = sEmail(i)
.lstvwContacts.Items(iCount).SubItems.AddRange(New ListViewItem.ListViewSubItem() {lstitm1, lstitm2, lstitm3})
Next
.lstContacts.EndUpdate()
iCount += 1
End While
sr.Close()
End If
End With
End Sub
So what it does, is it reads the contents of a text file and then loads all the info into a listarray, then I add those contents into a listview. Whats happening is:
say I have
123 123 123 123
1234 1234 1234 1234
in my Text File
When It loads to my ListView it does this
123 123 123 123
1234 1234 1234 1234
1234
It adds the Last Item Twice, but, only in the first column.
Whats my Issue and how can I fix this?
-
Jan 27th, 2009, 01:42 PM
#2
Re: Reading to End of File Causing 1 Extra Date Piece
This probably isnt the issue but personally when I am doing For loops I use something like this:
vb Code:
For i As Integer = 0 To CollectionName.Length - 1
instead of what you are doing which is:
vb Code:
For i As Integer = 0 to UBound(CollectionName)
Might be worth trying to change it? I dunno...
its kind of hard to work out what on earth your app is doing as there is so much going on there. Is there any way you could chop it down a bit but without taking out anything important just so that its easier for us to look through?
-
Jan 27th, 2009, 02:22 PM
#3
Thread Starter
Hyperactive Member
Re: Reading to End of File Causing 1 Extra Date Piece
vb Code:
While (sr.EndOfStream <> True)
Dim sLine As String = sr.ReadLine
Dim sSI() As String = Split(sLine, "|")
sTitle.Add(sSI(0))
sFullName.Add(sSI(1))
sPhone.Add(sSI(2))
sExt.Add(sSI(3))
sFax.Add(sSI(4))
sAddress1.Add(sSI(5))
sAddress2.Add(sSI(6))
sAddress3.Add(sSI(7))
sCity.Add(sSI(8))
sState.Add(sSI(9))
sZipCode.Add(sSI(10))
sEmail.Add(sSI(11))
ContactInfo.Add(sTitle(iCount).ToString & "|" & sFullName(iCount).ToString & "|" & _
sPhone(iCount).ToString & "|" & sExt(iCount).ToString & "|" & _
sFax(iCount).ToString & "|" & sAddress1(iCount).ToString & "|" & _
sAddress2(iCount).ToString & "|" & sAddress3(iCount).ToString & "|" & _
sCity(iCount).ToString & "|" & sState(iCount) & "|" & _
sZipCode(iCount).ToString & "|" & sEmail(iCount).ToString)
What that Part Does is It will LOOP Through the Text FIle till End Of File, then I have Defined ArrayList sFullName all the way to sEmail and then It will Split out the "|" that are in the Text File.
It then adds all the Items that I split in to 1 ArrayList, ContactInfo.
vb Code:
For i As Integer = 0 To ContactInfo.ToArray.Length - 1
.lstContacts.Items.Add(sFullName(i))
.lstvwContacts.Items.Add(sTitle(i))
Dim lstitm1, lstitm2, lstitm3 As New ListViewItem.ListViewSubItem
lstitm1.Text = sFullName(i)
lstitm2.Text = sAddress1(i)
lstitm3.Text = sEmail(i)
.lstvwContacts.Items(iCount).SubItems.AddRange(New ListViewItem.ListViewSubItem() {lstitm1, lstitm2, lstitm3})
Next
This loops through all ContactInfo (The ArrayList, I added all Information Too) To get the Lowest through the Highest Info, then It will add them to a ListBox, which is .lstContacts, and a ListView which is .lstvwContacts, My Problem is .lstvwContacts.Items.Add(sTitle(i)) seems to be adding 1 more Title than Everything Else. So, example:
Mr Wesley 008
Mr. Wes 0008
Mr.
The spaces are say a ListView Detail View in WinForm
-
Jan 27th, 2009, 11:32 PM
#4
Re: Reading to End of File Causing 1 Extra Date Piece
Have you debugged your code? Set a breakpoint at the top and then step through it line by line. It will likely become clear when you do that because you can test the state before and after every step.
-
Jan 28th, 2009, 12:49 AM
#5
Thread Starter
Hyperactive Member
Re: Reading to End of File Causing 1 Extra Date Piece
Hi, I did set break points, and I found out the Next was Looping Twice only @ the last bit, but I can't figure out why, this is bugging me -.- I also tried doing a For Each Loop but can't get the right control for a ArrayList Item.
-
Jan 28th, 2009, 04:13 AM
#6
Re: Reading to End of File Causing 1 Extra Date Piece
If ContactInfo is an ArrayList then why are you using ContactInfo.ToArray in the loop :S
Why not just:
vb Code:
For i As Integer = 0 To ContactInfo.Length - 1
I dont know if that will make any difference to your problem (i doubt it) but just thought it might be worth mentioning 
Also when you say you cant get the right control for an ArrayList item, the ArrayList will hold items of whatever type you added to it. So if you added a load of strings to the ArrayList then you would do:
vb Code:
For Each something As String In MyArrayList
-
Jan 28th, 2009, 10:16 AM
#7
Thread Starter
Hyperactive Member
Re: Reading to End of File Causing 1 Extra Date Piece
Still doesn't fix my issue.
vb Code:
For i As Integer = 0 To ContactInfo.ToArray.Length - 1
.lstContacts.Items.Add(sFullName(i))
.lstvwContacts.Items.Add(sTitle(i))
Dim lstitm1, lstitm2, lstitm3 As New ListViewItem.ListViewSubItem
lstitm1.Text = sFullName(i)
lstitm2.Text = sAddress1(i)
lstitm3.Text = sEmail(i)
.lstvwContacts.Items(iCount).SubItems.AddRange(New ListViewItem.ListViewSubItem() {lstitm1, lstitm2, lstitm3})
Next
-
Jan 28th, 2009, 10:23 AM
#8
Re: Reading to End of File Causing 1 Extra Date Piece
How can you say it doesnt fix your issue, then post code that doesnt implement either of the things I suggested?
-
Jan 28th, 2009, 10:52 AM
#9
Thread Starter
Hyperactive Member
Re: Reading to End of File Causing 1 Extra Date Piece
For i As Integer = 0 To ContactInfo.ToArray.Length - 1
I needed to add .ToArray because .Length wasn't showing up on Contact Info.
The For Each i as String In ContactInfo didn't work, because I couldn't be converted to a INteger, and then if I did For Each i as Integer In ContactInfo, it said the TEXT Like the Example Text couldn't be converted to a String. I tried both ways, its not working.
-
Jan 28th, 2009, 11:06 AM
#10
Re: Reading to End of File Causing 1 Extra Date Piece
Ah sorry its .Count for an ArrayList, not .Length, so you could just do
vb Code:
For i As Integer = 0 To ContactInfo.Count - 1
Anyway that shouldnt be making any difference.
I think what you need to do is step through your code in the debugger as JMC mentioned. Use breakpoints and the Step Into function (F8 key usually does this) and check to see why your ContactInfo collection has one more item in it than you are expecting. It must have one extra item for it to execute the loop an extra time. If you step through line by line and check the contents of the ContactInfo list constantly you can see when this additional item is added to it. Maybe someone with a little more experienc than me can tell just by looking at your code where you are going wrong but I think without seeing your entire app the only thing I can suggest is to step through it and see whats going on.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|