Error while working with array.

jdjd1118

Member
Joined
Nov 30, 2005
Messages
12
Programming Experience
Beginner
I am trying to take text from a txt file and split it into an array so I can then place each piece of text into three seperate list boxes. I come up with an error when at Safe(x).Username = ar(1). The error is as follows:

An unhandled exception of type 'System.IndexOutOfRangeException' occurred in system.windows.forms.dll

Additional information: Index was outside the bounds of the array.

I will put the code below:


If File.Exists(SafeFile) Then

Dim ar(3) As String
Dim SR As StreamReader
Dim STR As String
SR = File.OpenText(SafeFile)

Do Until SR.Peek = -1

STR = SR.ReadLine
ar = STR.Split(vbTab)

'add The websites
Safe(x).Website = ar(0)
LstWebSites.Items.Add(Safe(x).Website)
'Add the usernames

Safe(x).Username = ar(1)
LstWebSites.Items.Add(Safe(x).Username)
'add the passwords.

Safe(x).Password = ar(2)
LstWebSites.Items.Add(Safe(x).Password)
x += 1
Loop
Thank you for your help.
sorry, posted in wrong section
 
Last edited:
This would suggest that your array only has one element. That suggests that the line you have read does not contain any Tab characters. You should place a breakpoint in your code and examine the string that you've read from the file.

Also, note that vbTab, like all those vb constants, is actually a String object. You should be using ControlChars.Tab, which is a Char object, which is what the String.Split method expects. As it is your code is performing an implicit conversion at run time. Implicit conversions slow executuion of your code and can lead to errors and exceptions at run time. I strongly suggest, as I do to everyone, that you look into Option Strict and have it turned On always.
 
Back
Top