You've made a couple of mistakes which is why you are not getting the expected result.
When checking the bounds of an Array you need to specify the Array variable, in this case, the variable generated by Split()
which is CitizenshipCountry
.
Array elements are accessed by specifying the element ordinal position in parentheses ((...)
) not square brackets ([...]
).
Try this:
<%
Dim SelectedCountries, CitizenshipCountry, Count
SelectedCountries = "IN, CH, US"
CitizenshipCountry = Split(SelectedCountries,", ")
'Get the count of the array not the string.
Count = UBound(CitizenshipCountry)
'Use (..) when referencing array elements.
Call Response.Write(CitizenshipCountry(0))
Call Response.End()
%>
What I like to do is use IsArray
to check the variable contains a valid array before calling UBound()
to avoid these types of errors.
<%
Dim SelectedCountries, CitizenshipCountry, Count
SelectedCountries = "IN, CH, US"
CitizenshipCountry = Split(SelectedCountries,", ")
'Get the count of the array not the string.
If IsArray(CitizenshipCountry) Then
Count = UBound(CitizenshipCountry)
'Use (..) when referencing array elements.
Call Response.Write(CitizenshipCountry(0))
Else
Call Response.Write("Not an Array")
End If
Call Response.End()
%>
Split()
is working and creating an array calledCitizenshipCountry
. The issue is you then try to work out the number of elements of a string variableSelectedCountries
instead of checking the arrayUBound(CitizenshipCountry)
. Also, VBScript arrays use curly brackets like this not square ones like other languagesCitizenshipCountry(0)
.{ }
) anywhere?....I didn't think so, thanks for the edit anyway.