Link to home
Start Free TrialLog in
Avatar of Mr_Fulano
Mr_FulanoFlag for United States of America

asked on

Array limits gives Error = "Index was outside the bounds of the array.."

I have a character array that I step through and read each item in the array, which I call (i) -- That part works is OK.   However, now I also want to read the array item "one step ahead of (i)", which I call (j) at the same time I'm reading (i), so that I can make a decision, based on what comes next after (i).   However, I'm getting two different problems.

Problem 1: -- Using (aryTextToCheck.Length - 1) as my upper array limit:
I get an error that states:     "Index was outside the bounds of the array" when it tries to read (j).

i = 0
j = 0
For i = j To aryTextToCheck.Length - 1
       strChar = (aryTextToCheck(i))
       j = j + 1
           If j < aryTextToCheck.Length Then
               strNextChar = (aryTextToCheck(j))    <<<  I get this error here...."Index was outside the bounds of the array"
           End If
There is more code beyond this line, but the error occurs above as indicated.

Problem 2: Using (aryTextToCheck.Length) as my upper array limit to avoid stepping outside of the array limits:
This doesn't give me the error above, but it doesn't read (j) to the end of the array either.

i = 0
j = 0
For i = j To aryTextToCheck.Length - 1
       strChar = (aryTextToCheck(i))
       j = j + 1
           If j < aryTextToCheck.Length Then
               strNextChar = (aryTextToCheck(j))   <<<
           End If
As mentioned above, there is more code beyond this line, but the error occurs above that part of the code.

How can I avoid the stepping out of bounds on the array?

Thanks,
Fulano
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

If j < aryTextToCheck.Length - 1 Then
               strNextChar = (aryTextToCheck(j))    <<<  I get this error here...."Index was outside the bounds of the array"
End If
ASKER CERTIFIED SOLUTION
Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mr_Fulano

ASKER

Thanks WebTubbs, that sounds like a better idea than what I was trying to do, which was getting ahead of the (i) regardless of what (i) was at any point in time.

Thanks,
Fulano