Forum Moderators: open

Message Too Old, No Replies

ASP Variable Question

         

kevinj

4:54 pm on Jan 7, 2003 (gmt 0)

10+ Year Member



Does anyone know of a way to dynamically create variables in ASP using a loop? The code I've written that doesn't work is as follows:

For x = 1 to ROOMS

ROOM_TYPE & x =Trim(Request("ROOM_TYPE" & x))
NUMBER_IN_ROOM & x =Trim(Request("NUMBER_IN_ROOM" & x))
OCCUPANT_NAMES & x =Trim(Request("OCCUPANT_NAMES" & x))
ARRIVAL_DATE & x =Trim(Request("ARRIVAL_DATE" & x))
DEPARTURE_DATE & x =Trim(Request("DEPARTURE_DATE" & x))

Next

The dynamically created variables will be assigned the values of a form I created that posts the information to this page.

Thanks for any suggestions.

Dreamquick

5:15 pm on Jan 7, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmmm how about a dynamic array (since you cant actually dynamically declare variable names)


'NB Assuming that ROOMS is an number
Dim sRoomData()

Const ciRoomType = 0
Const ciRoomOccupants = 1
Const ciOccupantNames = 2
Const ciArrivalDate = 3
Const ciDepartureDate = 4

ReDim sRoomData( 4, CLng( ROOMS ) - 1 )

For x = 1 To CLng( ROOMS )

sRoomData( ciRoomType, x-1 ) = Trim( Request("ROOM_TYPE" & x) )
sRoomData( ciRoomOccupants, x-1 ) = Trim( Request("NUMBER_IN_ROOM" & x) )
sRoomData( ciOccupantNames, x-1 ) = Trim( Request("OCCUPANT_NAMES" & x) )
sRoomData( ciArrivalDate, x-1 ) = Trim( Request("ARRIVAL_DATE" & x) )
sRoomData( ciDepartureDate, x-1 ) = Trim( Request("DEPARTURE_DATE" & x) )

Next

Now in theory this would read in the form / querystring data and stash it away into a dynamically sized array. In other words the array is always the perfect size for the amount of data you are telling it you are going to give it...

Admittedly perhaps not as cute as the same approach using dynamic variables and/or separate arrays but it does work quite neatly and is nice and easy to maintain.

<notes>Added the constants to make it a little cleaner</notes>

-Tony

kevinj

3:40 am on Jan 8, 2003 (gmt 0)

10+ Year Member



Thanks Tony. I'm a novice when it comes to arrays but I'll try and get it to work using your idea.