Purvi
asked on
Generating variables dynamically
Hi,
I want to assign some value to a variable..This variable name is not predefined, variable should be created dynamically.
The problem is ASP doesn't allow any expression on left side of assignment operator..
This is my code.. I want to print "25" 5 times..
<%
For i=1 to 5
"a"&i=25
Response.write eval("a"&i)
Next
%>
Please Help
Thanks
-Purvi
I want to assign some value to a variable..This variable name is not predefined, variable should be created dynamically.
The problem is ASP doesn't allow any expression on left side of assignment operator..
This is my code.. I want to print "25" 5 times..
<%
For i=1 to 5
"a"&i=25
Response.write eval("a"&i)
Next
%>
Please Help
Thanks
-Purvi
My bad.
You'd have to change that to either:
redim a(6)
or
for i = 0 to 4
for the code to work
You'd have to change that to either:
redim a(6)
or
for i = 0 to 4
for the code to work
The problem with creating variable names dynamically (not predefined) is inconsistency in your code. When you create a variable generally you will need to call on that variable later on in your code. If the variable name is dynamic then that increases the chance for error on calling the correct name later. Henceforth the reason for using <% Option Explicit %>.
Purvi, Have you ever received an answer you thought was sufficient at this site? I ask because of your unusually bad habit of giving bad grades.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here's a cool trick: automatically assign all members of the Form collection to variables:
For Each item In Request.Form
' Create variable with the form item name, and assign it the item value
Execute("Dim " & item)
Execute(item & " = Request.Form(""" & item & """)")
Next
For Each item In Request.Form
' Create variable with the form item name, and assign it the item value
Execute("Dim " & item)
Execute(item & " = Request.Form(""" & item & """)")
Next
Purvi
Last 10 Grades Given C B C C C C C C C C
Thats too darn bad ! I wont even read a question by such a person.
Last 10 Grades Given C B C C C C C C C C
Thats too darn bad ! I wont even read a question by such a person.
You could:
use arrays, as everyone has suggested. And peterhiggens, declaring a vbscript array as array(5) creates a 6 element array, starting at 0 and going all the way up to 5. It's different to a lot of other languages, but that's how they work!
Create a dictionary object. This is similar to an array, and may not be the solution you're looking for, but can be quite nice.
That's about it. I like the sound of the Execute command, but could get messy with unknowns. Especially if you want to declare your variables (OPTION EXPLICIT) beforehand.
use arrays, as everyone has suggested. And peterhiggens, declaring a vbscript array as array(5) creates a 6 element array, starting at 0 and going all the way up to 5. It's different to a lot of other languages, but that's how they work!
Create a dictionary object. This is similar to an array, and may not be the solution you're looking for, but can be quite nice.
That's about it. I like the sound of the Execute command, but could get messy with unknowns. Especially if you want to declare your variables (OPTION EXPLICIT) beforehand.
ASKER
i know in this example i can use arrays..
but my actual problem is creating dynamic arrays..
if i can create dynamic variables then i can create dynamic arrays also.. Example given was just a simplied version of problem i was facing..
& for giving bad grade.. believe me whenever i use to get good answer i tried to give grades. but I couldn't find out where to give this grade..
I appologise for this.. but this time i'll make sure that proper grade is given.. i just use to accept the answer, i don't know how the grade is given.. really sorry for that..
-Purvi
but my actual problem is creating dynamic arrays..
if i can create dynamic variables then i can create dynamic arrays also.. Example given was just a simplied version of problem i was facing..
& for giving bad grade.. believe me whenever i use to get good answer i tried to give grades. but I couldn't find out where to give this grade..
I appologise for this.. but this time i'll make sure that proper grade is given.. i just use to accept the answer, i don't know how the grade is given.. really sorry for that..
-Purvi
ASKER
Hi drittich,
Thanks.. I think your solution will solve my problem..
In case of any problem i'll revert back..
Thanks once again..
Now i m TRYING to find out the way of giving GRADES :)
-Purvi
Thanks.. I think your solution will solve my problem..
In case of any problem i'll revert back..
Thanks once again..
Now i m TRYING to find out the way of giving GRADES :)
-Purvi
ASKER
Hi drittich,
Thanks.. I think your solution will solve my problem..
In case of any problem i'll revert back..
Thanks once again..
-Purvi
Thanks.. I think your solution will solve my problem..
In case of any problem i'll revert back..
Thanks once again..
-Purvi
Thanks drittich, This solved my problem too :D
<%
dim a()
redim a(5)
For i=1 to 5
a(i) = 25
Response.write (a(i))
Next
%>