Skip to main content
January 3, 2011
Answered

"Variable" variable names

  • January 3, 2011
  • 1 reply
  • 421 views

I'm trying to set up a system of Client variables that are similar but unique for separate applications. My initial idea was to prepend the Application name to the client name, e.g., AppA_UserName. I tried unsucessfully to use the application variable as <cfset #AppName#_UserName = "John">.

Is there a way to do this?

    This topic has been closed for replies.
    Correct answer Adam Cameron.

    It's in the docs:

    http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec09d55-7fec.html

    Dynamic variables are variables that are named dynamically, typically by creating a variable name from a static part and a variable part. For example, the following example dynamically constructs the variable name from a variable prefix and a static suffix:

    <cfset "#flavor#_availability" = "out of stock">

    Or (IMO better/clearer), leveraging the fact that a struct key is just a string, and a variables-scoped variable is just a key in the variables struct:

    <cfset variables["#flavor#_availability"] = "out of stock">

    It's also covered quite comprehensively via a quick google search, which probably should have been your first port of call when the question arose (my positio being it's better & quicker to find the aswer for one's self, than ask someoe else):

    http://www.google.com/search?q=coldfusion+dynamic+variable+names&ie=UTF-8

    --

    Adam

    1 reply

    Adam Cameron.Correct answer
    Inspiring
    January 3, 2011

    It's in the docs:

    http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec09d55-7fec.html

    Dynamic variables are variables that are named dynamically, typically by creating a variable name from a static part and a variable part. For example, the following example dynamically constructs the variable name from a variable prefix and a static suffix:

    <cfset "#flavor#_availability" = "out of stock">

    Or (IMO better/clearer), leveraging the fact that a struct key is just a string, and a variables-scoped variable is just a key in the variables struct:

    <cfset variables["#flavor#_availability"] = "out of stock">

    It's also covered quite comprehensively via a quick google search, which probably should have been your first port of call when the question arose (my positio being it's better & quicker to find the aswer for one's self, than ask someoe else):

    http://www.google.com/search?q=coldfusion+dynamic+variable+names&ie=UTF-8

    --

    Adam

    January 3, 2011

    You are corrrect, I did Google first, but it only works if you pose the question "correctly". Also, I am still on CF7 so I didn't refer to the CF9 manual. Thanks again.

    Inspiring
    January 3, 2011

    The information Adam gave you, like most of the information in the CF9 documents, is also valid for CF7.