By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,649 Members | 1,816 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,649 IT Pros & Developers. It's quick & easy.

Unique Variables inside loop

P: 4
I have a form with 30 input boxes named
box1, box2, box3, etc.

Instead of:
var1 = Request.Form("box1")
var2 = Request.Form("box2")
etc.
..I am trying to collect the values in a loop statement and assign a unique variable but it is not working:

for x = 1 to 30
var & x = Request.Form("box"&x)
next

Does anyone know how to solve this problem?
Oct 26 '06 #1
Share this Question
Share on Google+
6 Replies


P: 7
have you tried using an array with each index corresponding to each form element?
Oct 26 '06 #2

P: 4
have you tried using an array with each index corresponding to each form element?
I am not sure if this is what you mean but I tried this array however, as soon as I go out of the array, the variable is no longer around.

dim box(30)
for x = 1 to 30
var(x) = Request.Form("box"&x)
'** inside here the var1 works
Response.Write var1
next

'**Out here var1 does not return anything..
Response.Write var1 'has no value

Any other thoughts?
Oct 27 '06 #3

P: 4
I am not sure if this is what you mean but I tried this array however, as soon as I go out of the array, the variable is no longer around.

dim box(30)
for x = 1 to 30
var(x) = Request.Form("box"&x)
'** inside here the var1 works
Response.Write var1
next

'**Out here var1 does not return anything..
Response.Write var1 'has no value

Any other thoughts?
Sorry I need to correct myself.
Inside the array Response.Write var1 does not work.
Only Response.Write var(x) returns the results.
How can I "assign" the variables to return the results outside the array like so:
Response.Write var1 & "<br>"
Response.Write var2 & "<br>"
etc.
Oct 27 '06 #4

P: 7
I am not sure if this is what you mean but I tried this array however, as soon as I go out of the array, the variable is no longer around.

dim box(30)
for x = 1 to 30
var(x) = Request.Form("box"&x)
'** inside here the var1 works
Response.Write var1
next

'**Out here var1 does not return anything..
Response.Write var1 'has no value

Any other thoughts?
I don't know if you accidentally have your variables switched but this is how it should look.

dim var(30) as String 'you can make this an object array then typecast as needed

for int as x = 1 to 30
var(x-1) = Request.Form("box"&x) 'when working with arrays always start at index=0
'** inside here the var1 works
Response.Write var(x-1)
next

Also make sure that if you declare a variable inside one function it will be unaccessable to another unless you pass it as an argument or declare it globally. Additionally if a variable is created inside function1(), it will be destroyed once the program exits function1().
Oct 27 '06 #5

P: 4
I don't know if you accidentally have your variables switched but this is how it should look.

dim var(30) as String 'you can make this an object array then typecast as needed

for int as x = 1 to 30
var(x-1) = Request.Form("box"&x) 'when working with arrays always start at index=0
'** inside here the var1 works
Response.Write var(x-1)
next

Also make sure that if you declare a variable inside one function it will be unaccessable to another unless you pass it as an argument or declare it globally. Additionally if a variable is created inside function1(), it will be destroyed once the program exits function1().

Yes, I accidentally goofed up on the variable too. Should have started out with proper code. Per your example

The Response.Write var(x-1) works inside the array but I am looking for a way to assign the variable var1, var2 etc (which are the var(x)) so I can use it anywhere in the page.

Can this be accomplished? I hope it makes sense.
Oct 27 '06 #6

P: 7
Simply declare your array outside any methods, but inside the class and it should work. :)

Expand|Select|Wrap|Line Numbers
  1. Partial Class MyDocument
  2.     Inherits System.Web.UI.Page
  3.  
  4.     Private Shared var(30) as String
  5.  
  6.     method1()
  7.     method2()
  8. End Class
Oct 27 '06 #7

Post your reply

Sign in to post your reply or Sign up for a free account.