Dynamically Named Variables

Results 1 to 3 of 3

Thread: Dynamically Named Variables

  1. #1
    azure@nospam.eprise.com Guest

    Default Dynamically Named Variables

    Is there a way to create dynamically named variables in ASP?<BR><BR>For instance, let&#039s say we have form variables submitted to a page and you want to create a variable for each one of those form variables...<BR><BR>FOR EACH ITEM IN Request.Form<BR> &#060;variable name based on ITEM&#062; = Request.Form(Item)<BR>Next<BR><BR>If you have an answer, and if possible, please respond to my email address above. Remove the "nospam" part.

  2. #2
    Join Date
    Dec 1969

    Default RE: Dynamically Named Variables

    I would really like to know an answer to this question if anyone has a solution! Please e-mail ewitch@yahoo.com - thanks

  3. #3
    Join Date
    Dec 1969

    Default Really a silly thing to do, but...

    Yes, in VBScript version 5 you could probably do this.<BR><BR>Basically, you&#039d do an "eval" call (read up on eval in the VBS version 5 docs).<BR><BR>BUT WHY???<BR><BR>Once you have done this, what in the world are you going to *DO* with these variables????<BR><BR>The rest of your code won&#039t know what their names are (since you created them dynamically), so how can you use or manipulate the values you went to the trouble to store in them???<BR><BR>Almost surely what you should be doing is using a Dictionary object or an array.<BR><BR>You could do:<BR><BR>&#060;%<BR>Set dict = Server.CreateObject("Script.Dictionary")<BR>FOR EACH ITEM IN Request.Form<BR>&nbsp;&nbsp;&nbsp;&nbsp;dict.Add ITEM, Request.Form(Item)<BR>Next<BR>%&#062;<BR><BR>and now you can access the contents of that dictionary in *MANY* ways. I&#039ll let you read up on that object to see what I mean.<BR><BR>The alternative is an array:<BR><BR>&#060;%<BR>Dim theInfo( )<BR>ReDim theInfo( 2, Request.Form.Count )<BR>itemNum = 1<BR>For Each Item in Request.Form<BR>&nbsp;&nbsp;&nbsp;&nbsp;theInfo( 1, itemNum ) = Item<BR>&nbsp;&nbsp;&nbsp;&nbsp;theInfo( 2, itemNum ) = Request.Form(Item)<BR>Next<BR>%&#062;<BR><BR>and then you can access the elements dynamically. (I used elements 1 through n; VBS allows you to use 0 through n. Wasting the 0 elements is noise, so do as you please.)<BR><BR>The array doesn&#039t strike me as being as convenient as the dictionary, but either of them allow you future dynamic access to their contents, which I can&#039t see how you will get by creating new variables that nothing else in the program can know about without going through the same process.<BR><BR>Oh...and the rules are: Post here, get your answer here. Sorry, but until Scott gets "post reply and email to requestor" to this forum, it&#039s just too much trouble to do both.<BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts