Help please - Dynamically Generated Variable Names

Results 1 to 4 of 4

Thread: Help please - Dynamically Generated Variable Names

  1. #1
    Fred Guest

    Default Help please - Dynamically Generated Variable Names

    Is it possible to dynamically generate variable names in a loop(i.e. Name01, Name02, Name03, etc where Name01 is "Name" & counter), populate them, and then later reference them? If so, how is it done? I would be referencing them later in a loop. I know this is a perfect application for an array, but I cannot use one since I don&#039;t know in advance how may items there will be in the loop.<BR><BR>Thank you in advance for your time.

  2. #2
    Join Date
    Dec 1969

    Default Linked List maybe???

    I know in other languages (C, C++, Java etc..) you can generate a linked list where you have a group variable with two parts to it...<BR><BR>say you have a variable... You have the data portion of the variable and a pointer...<BR>Your pointer will point to another variable of the same type...<BR><BR>The basic idea is you are creating something like this...<BR><BR>Variable --------&#062;Variable<BR>value1 &#124 value1<BR>Pointer ----- NEW Pointer-----&#062;NULL<BR><BR>Every time you add something to the linked list you create a new variable and set the pointer equal to the memory location of the new pointer<BR><BR>I am not sure about how to go about workin with this in ASP/VBScript as I have not attempted it, maybe someone will pick this up and explain. I am also going to do some searching and get back to this topic. Hang in there, there is a solution...

  3. #3
    Join Date
    Dec 1969

    Default Dynamic names *ALWAYS* a mistake...

    Nobody has *EVER* yet shown me a case where they make sense!<BR><BR>You say you don&#039;t know in advance how many values you will have.<BR><BR>So you will have the same code complication with multiple names as you will with the array. But more so.<BR><BR>But arrays in VBScript *CAN* BE RESIZED!!!!<BR><BR>Look up the docs on ReDim Preserve !!!<BR><BR>And for a way to manage "growing" an array dynamically in an *efficient* fashion, see the ASPFAQs, topic Arrays, and the question re arrays in sesssion/application variables. Even if you won&#039;t put the array in such a variable, the way to dynamically add elements to an array is the *right* way to go!<BR><BR>*** THERE IS ANOTHER WAY ***<BR><BR>Allocate many, many more elements in the array than you need!<BR><BR>Each empty array element eats only 16 bytes of memory, so a thousand unused array elements is only 16KB of memory!<BR><BR>Then just keep track of where you are with a separate variable.<BR><BR>You *can* shrink the array to the right size when you are done (remember ReDim preserve!), but you don&#039;t have to.<BR><BR>Caution: To use ReDim Preserve, you need to have *created* the array with ReDim.<BR><BR>Thus:<BR><BR>Dim myArray() &#039; no size given!<BR>ReDim myArray( 100 )<BR><BR>... later, you run out of room ...<BR><BR>ReDim Preserve myArray( 200 )<BR><BR>... later, you want to shrink it ...<BR><BR>ReDim Preserver myArray( actualSize )<BR><BR>

  4. #4
    Fred Guest

    Default Thank you Bill for your answer

    You have convinced me to use an array. Thank you also for the pointing me to the FAQs I needed.<BR><BR>Fred

Posting Permissions

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