Error 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.
 
Hi, Not too hot on this subject but from what i can see, you have an array:-
ar(3)
But you are only using ar(0), ar(1), ar(2) so i think that should be :-
ar(2)
Also, vbtab is your delimeter in the string, but is it possible that you have some leading or trailing spaces in your string so you are getting an empty string returned to the array..

As i say i'm not great at this but i hope that i've helped. If not Sorry!!:)
 
vis -no.... that's not the way arrays work in .NET... you declare the number of elements you need. In this case 3. But the elements are numbered 0-2, so that much is right.

However, I've never used split with set Array sizes. Usualy they are dimed as dynamic arrays. The other thing..... check ar(0) to make sure it is in fact spliting properly. if ar(1) is out of bounds, then I'd have to look at the aplit to see if it really is spliting like it should. Wouldn't surprise me to see the entire contents stuffed into ar(0).

-tg
 
well jdjd1118 i guess i was a bit off!!:) . Hope i didn't mislead you, thanks for setting that right TG.
What can i say i took a shot and sadly, i was a mile wide!!!
 
hey u have not mentioned the size of the split
according to me the split u have written has some ting missing probably it should be dim ar as string()
ar = STR.Split(vbTab,3) instead ar = STR.Split(vbTab) ok.
Then ; why r u using Safe(x).Website = ar(0) what is Safe(x) -- u can do is say dim Safe1,Safe2,Safe3 as string
then Safe1=sr(0)
 
Back
Top