troubleshooting Question

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

Avatar of Mr_Fulano
Mr_FulanoFlag for United States of America asked on
Visual Basic.NET
3 Comments1 Solution301 ViewsLast Modified:
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
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros