tags:

views:

115

answers:

3

Im looking for help with trying to vary a variable-name in a for loop:

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

Thanks in advance as I can't logon to accept an answer...

+1  A: 

Do not have an OpenID to logon, but you can just ask a question with an e-mail address here - without a logon. So I end up replying in the answer section... Am a nobb.

Why so may view on the post too... Is it a totally silly question or something?

carpenter
What is the question, exactly?
asbjornu
+4  A: 

I don't know why you think that you need a separate variable for each instance. The variable just holds a reference to the object, it doesn't matter at all what you call the variable. You can just reuse the same variable for each of the objects:

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
Guffa
I have no idea why this was voted down; Guffa is completely correct here. The name of the variable is irrelevant, since it only exists within the context of a single iteration of the loop anyway. Additionally, carpenter's original code in the question itself is creating an array in each iteration of the loop for absolutely no reason!
Chris
A: 

Thanks Guffa - my pics are now making it through to the email and showing up.

The (b) in the image_var(b) was just a stub for me too - till I found the code I was after... Am new and didn't even know/realise that it made an array... am a nobb.

Thanks again...

carpenter
I did reckon that you meant it as pseudo code for some dynamic variable name, not really an array. (You can't just create an array by adding an index to a variable anyway, you have to declare it as an array to create one.)
Guffa