在for循环中改变变量名

Car*_*ter 1 vb.net asp.net

我正在寻找帮助尝试在for循环中改变变量名:

For b = 1 To Count

    ' the below image_var(b) varible needs to vary on each iteration'
    Dim image_var(b) As New LinkedResource(Server.MapPath(myArray(b-1)))

    ' need also to have the b in myArray(b-1) bit work through the loop too'
    ' that is I''m after the array index b minus 1'

    image_var(b).ContentId = "imageContentId_" + b.ToString

    ' add the LinkedResource to the appropriate view'
    htmlView.LinkedResources.Add(image_var(b))

Next b
Run Code Online (Sandbox Code Playgroud)

提前谢谢,因为我无法登录接受答案......


谢谢Guffa - 我的照片正在通过电子邮件显示出来.

image_var(b)中的(b)对我来说也只是一个存根 - 直到我找到了我所追求的代码......我是新的,甚至不知道/意识到它创建了一个数组......我是一个nobb .

再次感谢...

Guf*_*ffa 5

我不知道你为什么认为每个实例需要一个单独的变量.变量只保存对象的引用,你所谓的变量根本不重要.您可以为每个对象重用相同的变量:

For b = 1 To Count

  Dim image As New LinkedResource(Server.MapPath(myArray(b-1)))

  image.ContentId = "imageContentId_" + b.ToString

  ' add the LinkedResource to the appropriate view'
  htmlView.LinkedResources.Add(image)

Next b
Run Code Online (Sandbox Code Playgroud)