0

I'm hoping I could maybe get some help with this small problem. Basically I'm trying to access the elements of a string array in vb.net using a negative number.

Here is the problematic piece of code:

If p < filestring.Length Then
                c = (c << 8) Or Asc(filestring(p))
                p += 1
                Dim m = (c >> 3) And 2047
                Dim n = (c And 7) + 3
                If (m > n) Then
                    o += o(-m)
                    o += o(n - m)
                Else
                    For value = 0 To n
                        o += o(m)

                    Next

                End If

I get an index out of range exception when I try to use -m to access an array element. -m is always negative and always smaller than the size of the string array yet I still get that error. Does anyone have any suggestions on how to work around this please?

I should have added that I'm trying to port python code to vb.net so the original python version of the above would look like this:

            if p < len(i):
                c = (c << 8) | ord(i[p])
                p += 1
                m = (c >> 3) & 0x07ff
                n = (c & 7) + 3
                if (m > n):
                    o += o[-m:n-m]
                else:
                    for _ in xrange(n):
                        o += o[-m]

I hope this maybe sheds some more light on the problem and apologise for not posting it the first time around.

6
  • 1
    Well - seeing as all indexes are positive, I don't know why you try to use a negative number to access an index? Commented Jan 16, 2014 at 11:45
  • "...-m is always negative...": there, you answered it yourself! Commented Jan 16, 2014 at 11:47
  • I never thought about it, but this could be confusing for someone coming from a language like Python, which allows negative indexes. Commented Jan 16, 2014 at 11:59
  • When I try to use m as a positive number I don't get the out of range exception but I don't get the output expected. Essentially the code needs to read so many places backwards before reading a certain amount forward from that position. Commented Jan 16, 2014 at 12:07
  • if possible, try to replace negative number indexes with (negative number + array size), since "l[-n] is shorthand for l[len(l)-n] in python". reference Commented Jan 16, 2014 at 12:34

2 Answers 2

1

It's not possible, array indexes are never negative. Think of them as ofsets from the first element. So 0 is the first element and so on. If you have a negative number this would be before the first number. I think that you are doing something strange in your code when you need to retrieve something with a negative index.

Perhaps you should access yourArray(yourArray.Length - yourOfset)

Microsoft documentation

I don't know Python but looking here I would translate o[-m:n-m] to o.GetRange(o.Length - m, n - m).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Zache but when I try that I still get the out of range exception.
1

VB6 used to allow negative (and non zero) based arrays so you could do this:

Dim arr(-34 To 34) As String
arr(-21) = "Hello"

or this

Dim arr(55 To 128) As String
arr(99) = "Hello"

But this is not allowed in .NET. This enforces zero based arrays in all cases.

Was this code upgraded from VB6? (I'm guessing it was upgraded from something as this would not work in .NET) If so then you need to adjust you array indexes so that they all start at zero. A simple offset from your lowest negative array index to zero should suffice.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.