2

I am trying, without success, to iterate through an array of varying varTypes in vbScript.

NOTE: I am talking about a SINGLE dimensioned array, not a vbScript multi-dimensional array.

I can successfully create a test array as follows:

Dim aArray : aArray = array("A", "B", array(1,2,3,4), "D", array("have", "a", "happy", "holiday", "!"), 1, "last item")

As you can see, the primary array has items of varying types, some strings, some integers, and some arrays. Then, when looping, all goes well until iterating to the array item which holds another array.

I need to know what to do when I come to this part:

If typeName(aArray(i)) = "Variant()" Then

Here is the complete function to iterate and print the array:

For i = 0 to uBound(aArray)
    If typeName(aArray(i)) = "Variant()" Then
        'array item is another array e.g. vbScript typeName variant() 
        response.write "<tr><td>aArray(" & i & ")</td><td><table class=""debugVarsTbl""><tr><th>Value</th><th>typeName()</th></tr><tr><td>[Array()]</td><td>" & typeName(aArray(i)) & "</td></tr></table></td></tr>"
    Else
        'item is a standard string, integer, boolean, date, etc.
        response.write "<tr><td>aArray(" & i & ")</td><td>" & aArray(i) & " (" & typeName(aArray(i)) & ")</td></tr>"
    End If
Next

I basically need to know how to access the main array item which is of vbScript typeName = Variant().

Thanks in advance and happy holidays, CBWDEV

4
  • I take it this is ASP? Added the tag, sorry if it doesn’t apply :) Commented Dec 25, 2012 at 15:21
  • "I am trying, without success, to iterate through an array of varying varTypes in vbScript." This is VBScript en.wikipedia.org/wiki/VBScript Commented Dec 25, 2012 at 15:23
  • is the actual loop not working or the logic inside? Commented Dec 25, 2012 at 15:31
  • It is vbscript in classic ASP. The loop works, and it hangs up when i try to do something on the iteration which contains the nested array. Commented Dec 26, 2012 at 11:32

2 Answers 2

1
If typeName(aArray(i)) = "Variant()" Then

should be

If typeName(aArray(i)) = "Variant" Then

also if the output has common code it might be better to use a switch to work out what needs to change per type.

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

2 Comments

typeName(aArray(i)) actually returns "Variant()" when it is an array.
Also I am going to use your advice and implement a switch for the various cases.
1

Thanks all for the help and the pointers in the right direction. I found the solution. Here is a routine I use for debugging arrays:

Sub debugArray(byVal aArray)
    For i = 0 to uBound(aArray)
        If typeName(aArray(i)) = "Variant()" Then
            'array item is another array e.g. vbScript typeName variant() 
            response.write "<tr><td>aArray(" & i & ")</td><td><table class=""debugVarsTbl""><tr><th>Value</th><th>typeName()</th></tr><tr><td>" 
            debugArray aArray(i)
            response.write "</td><td>" & typeName(aArray(i)) & "</td></tr></table></td></tr>"
        Else
            'item is a standard string, integer, boolean, date, etc.
            response.write "<tr><td>aArray(" & i & ")</td><td>" & aArray(i) & " (" & typeName(aArray(i)) & ")</td></tr>"
        End If
    Next
End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.