在每个循环的经典ASP中动态分配值。

时间:2022-05-17 05:15:23

I'm trying to assign a value to a series of variables inside a FOR EACH loop, but keep getting a "Type mismatch" error.

我试图在每个循环a中为一系列变量赋值,但总是得到一个“类型不匹配”错误。

personOrder = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15" 'order items displayed onscreen

personArray = split(personOrder, ",")

For each i in personArray
    imageArray(i) = objContentXML.selectSingleNode("/page/profile" & i & "/image").text
Next

Note: I've made the var personOrder a list because in the future items might change order.

注意:我将var personOrder作为一个列表,因为在将来的项目中可能会改变顺序。

I think there's a problem with assigning array values in a FOR EACH loop. Annoyingly it works fine in a FOR loop, but I'm trying to future proof my code.

我认为在每个循环中分配数组值是有问题的。烦人的是,它在FOR循环中工作得很好,但是我正在尝试未来的代码。

Apologies if this question is too stupid. I'm returning after 9 months of nappies and burping to the world of code (less nappies, more burping).

如果这个问题太愚蠢,请道歉。在经历了9个月的尿布和打嗝之后,我回到了代码的世界(少了尿布,多了打嗝)。

1 个解决方案

#1


4  

On which statement do you get this error? I can execute the following code without any error messages:

你在哪个陈述上犯了这个错误?我可以执行以下代码,没有任何错误信息:

personOrder = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15" 'order items displayed onscreen

personArray = split(personOrder, ",")

dim imageArray
redim imageArray(uBound(personArray) + 1)

For each i in personArray
    imageArray(i) = i
Next

You must create an array first and declare the number of items you want to put in it (use redim to change the dimensions).

您必须首先创建一个数组并声明要放入其中的项的数量(使用重拨来更改维度)。

I replaced your objectContentXML with a simple statement, for testing purposes.

为了进行测试,我用一个简单的语句替换了objectContentXML。

#1


4  

On which statement do you get this error? I can execute the following code without any error messages:

你在哪个陈述上犯了这个错误?我可以执行以下代码,没有任何错误信息:

personOrder = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15" 'order items displayed onscreen

personArray = split(personOrder, ",")

dim imageArray
redim imageArray(uBound(personArray) + 1)

For each i in personArray
    imageArray(i) = i
Next

You must create an array first and declare the number of items you want to put in it (use redim to change the dimensions).

您必须首先创建一个数组并声明要放入其中的项的数量(使用重拨来更改维度)。

I replaced your objectContentXML with a simple statement, for testing purposes.

为了进行测试,我用一个简单的语句替换了objectContentXML。