Me.Controls equivalent for private variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • robertybob
    New Member
    • Feb 2013
    • 116

    Me.Controls equivalent for private variable

    Sorry if the title to this question didn't really make much sense - hard to think how to easily explain this :)

    If I want to target a label on a form I can use
    Code:
    dim labelname = "myLabel"
    Me.Controls(labelname).Text = "John"
    What do I use to target a variable I set for the form as, say, Private myName as String?

    The following fails so I assume these aren't controls but are something else?

    Code:
    dim thisname = "myName"
    Me.Controls(thisname).Text = "John"
    Many thanks!
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    There is no collection for variables. It is rare that someone would need to access a variable by its name. It usually means they are overcomplicatin g things when an easier solution exists such as an array. But we would need to know the purpose to offer any alternative suggestions.

    If you need to access variables by name, you should use a dictionary object instead.

    Comment

    • robertybob
      New Member
      • Feb 2013
      • 116

      #3
      Thanks Rabbit - will look at a dictionary solution.

      All the best.

      Comment

      Working...