SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    ********* Ornithologist AtomicPenguin's Avatar
    Join Date
    May 2002
    Location
    Vancouver, BC
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    dynamically creating variable names

    Hey guys!

    Nice to be back here.

    Here's my problem: I need to create a variable name dynamically, made up of a string (say "var_name_") and an integer. Ultimately I want something like this:

    var_name_1 = 'whatever value I want';
    var_name_2 = 'whatever value I want';
    var_name_3 = 'whatever value I want';
    ...
    var_name_N = 'whatever value I want';



    But the actual variable names are determined at run-time, so I can't just hardcode them.

    Any suggestions? JavaScript doesn't seem to have a Variable type, so I can't just cast a string into a var...

  2. #2
    SitePoint Enthusiast Zero G's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have two ideas for you.

    1) Only have one variable but make it an array. And use that to store all your values
    2) Use php to print out variables when the page loads.

    I had the same kind of problem while doing a visual basic application a while ago and ended up making an array to simulate the variables.


    I donít think JS has anything where you can make new variables at runtime

  3. #3
    SitePoint Member
    Join Date
    Aug 2003
    Location
    Netherlands
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could use eval(), I think:
    for (var i=0;i<9;i++){
    eval('var_name_'+i+'="whatever you want";');
    }
    But you could better use an array to store the variables in that case.

  4. #4
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    You might find the following helpful:
    Code:
    for (i = 0; i < 3; i++){
    x = ('var myVar' + i + ' = ' + i)
    eval(x)
    alert("test1: x is a " + typeof x + "\n x = '" + x + "'")
    }
    for (i = 0; i < 3; i++)
    alert('myVar' + i + ' = ' + eval('myVar'+i))
    for (i = 0; i < 3; i++)
    alert(eval('var myVar' + i + ' = ' + i))
    for (i = 0; i < 3; i++)
    alert('test2: myVar' + i + ' = ' + eval('myVar'+i))
    However, 'dynamic' does not always mean 'unknown', so what is you are doing exactly? For example: Why can't you grab the recordset length and use that to determine how many vars you need?

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  5. #5
    SitePoint Member
    Join Date
    Nov 2002
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could access each variable name as a string using the window object

    window["my_var"+x] = "";

    i guess declaring a variable in this manner without var could cause scope problems in functions etc though.

    Adam


Bookmarks

Posting Permissions

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