Question

I'm trying to ReDim an array in QTP/VB Script but I keep getting the error: Expected Statement.

I've been over the code a hundred times in the last few hours and I'm just not seeing what is missing, and nobody in the office here seems to know what is missing.

So this is just a sanity check for me at this point, and get a fresh set of eyes on the problem.

Here is the line of code with the error message:

 ReDim strTempArray(LBound(AryVar) To (UBound(AryVar) - 1))

Or I could put it this way, I've tried so many variations on the theme:

 ReDim strTempArray(0 To AryVar.Length - 1)

But they all have one thing in common, they all give the same meaningless error: Expected statement on that one line of code.

Alright, so maybe it's something with the rest of the sub, so here is the full sub code. Basically it's part of a Sub called RemoveArrayElement:

 Public Sub RemoveArrayElement(AryVar(), intIndexToRemove)
 strTempArray()
 lngX = 0 'As Long
 lngDestinationIndex = 0 'As Long

    ReDim strTempArray(LBound(AryVar) To (UBound(AryVar) - 1))
    'ReDim strTempArray(0 To AryVar.Length - 1)
    lngDestinationIndex = 0

    For lngX = LBound(AryVar) To UBound(AryVar)
        If lngX <> intIndexToRemove Then
            strTempArray(lngDestinationIndex) = AryVar(lngX)
            lngDestinationIndex = lngDestinationIndex + 1
        End If
    Next

    AryVar = strTempArray
 'End If
 End Sub

I should also add that I did include the Option Explicit at the beginning of the file.

Commenting out that one line that is giving the error does make the error go away, which seems to imply for me that the error really is on that line of code and not some place else.

So what am I missing?

Thanks in advance!

Was it helpful?

Solution

VBScript arrays are strictly zero-based, so specifying a range (.. To ..) makes no sense and isn't supported (in contrast to other Basic dialects). Just use ReDim Array(UBound) and be careful with your indices.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top