开发者

Assign one array to another Classic asp

开发者 https://www.devze.com 2023-03-18 02:59 出处:网络
I\'m pretty new to classic asp and working with arrays is not fun :( I keep getting an error on dim isMyApiResponse

I'm pretty new to classic asp and working with arrays is not fun :(

I keep getting an error on

            dim isMyApiResponse

            if MyAvailRooms = "Error" then
                isMyApiResponse = false
            else
                isMyApiResponse = true
                dim MyAvailArray()

                If InStr( MyAvailRooms, "," ) > 1 Then 
                    'response.write("<h2>MyAvailRooms is " & MyAvailRooms)
                    'response.write("<h2>var type of My avil rooms is " & VarType(MyAvailRooms))
                    MyAvailRooms = CStr(MyAvailRooms)
                    dim tempArray
                    tempArray = split(MyAvailRooms, ",")

                    dim sizeTempArray
                    sizeTempArray = UBound(tempArray)

                    response.write ("<h2>type of sizeTempArray is " &VarType(MyAvailArray))
                    response.write("<br/>type of temp array is " & VarType(tempArray))

                    reDim MyAvailArray(sizeTem开发者_如何学CpArray)
                    MyAvailArray  = tempArray

                else 
                    ReDim MyAvailArray(1)
                    MyAvailArray (0) = MyAvailRooms
                end if
            end if

I'm getting a type mismatch on the line "MyAvailArray = tempArray" above. I guess it's a dynamic fixed array problem or something. Both types are coming back as 8204 in my reponse.write, which is variant I think.

I'm at the end of my tether, please help me Classic ASP ninjas!


While I am not able to explain why this throws an error:

MyAvailArray = tempArray

You can loop through each array element and assign them one-by-one and it should work ;)

Dim i
for i = 0 to sizeTempArray
    MyAvailArray(i) = tempArray(i)
next


I've always had trouble declaring dynamic arrays using the Dim myArray() syntax. I always declare my arrays as regular variants, which still allows me to use the ReDim statement later on.

(As a side note, if I need an empty array, I'll use the Array function with no arguments, i.e. myArray = Array(). The benefit there is that calling the UBound function will return -1 instead of giving an error, so I can easily determine if an array is empty.)

Dim MyAvailArray

If InStr( MyAvailRooms, "," ) > 1 Then
    '<snip>'
    MyAvailArray = tempArray
Else 
    MyAvailArray = Array(MyAvailRooms)
End If

Note that I used the Array function for brevity.


By the way, the InStr check is unnecessary, since Split will always return an array, even if the separator isn't in the string. Therefore, you could rewrite the code as:

Dim MyAvailArray
MyAvailArray = Split(MyAvailRooms, ",")
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号