Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. looping through variables whose names start with...

looping through variables whose names start with...

Scheduled Pinned Locked Moved C#
phpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LordZoster
    wrote on last edited by
    #1

    Hallo i'd need to write a loop which iterate through the variables whose names start with a specific string: i.e.

    for every variable whose name starts with "PATH_.." [or Left(name,5) == "PATH_"]
    perform a file.exists check

    or, again

    for every variable whose name starts with "IMG_.."
    do this: imgArray[variable_name] = variable_value
    (imgArray being a pre defined Hashtable)

    To be clearer, in PHP variable names can be obtained through the syntax $$variable, where $variable returns the variable value only. In other words, i would like to automatically do a collection of variables grouped by particular strings in their names. Is it possible?

    L 1 Reply Last reply
    0
    • L LordZoster

      Hallo i'd need to write a loop which iterate through the variables whose names start with a specific string: i.e.

      for every variable whose name starts with "PATH_.." [or Left(name,5) == "PATH_"]
      perform a file.exists check

      or, again

      for every variable whose name starts with "IMG_.."
      do this: imgArray[variable_name] = variable_value
      (imgArray being a pre defined Hashtable)

      To be clearer, in PHP variable names can be obtained through the syntax $$variable, where $variable returns the variable value only. In other words, i would like to automatically do a collection of variables grouped by particular strings in their names. Is it possible?

      L Offline
      L Offline
      LordZoster
      wrote on last edited by
      #2

      To give the whole look, i'll try to explain what i want to do. In the application, according to the user's choice, the main form backgroundImage changes, and is programmantically choosen among several external images whose paths are specified as user settings in the app.config: they are 10-15, number could change, those paths names all start with "PATH_". So first of all i'd like to check if those files exists. And then i'd like to change the main form background image at runtime according the current operation. Maybe there's a different way to reach this?

      L 1 Reply Last reply
      0
      • L LordZoster

        To give the whole look, i'll try to explain what i want to do. In the application, according to the user's choice, the main form backgroundImage changes, and is programmantically choosen among several external images whose paths are specified as user settings in the app.config: they are 10-15, number could change, those paths names all start with "PATH_". So first of all i'd like to check if those files exists. And then i'd like to change the main form background image at runtime according the current operation. Maybe there's a different way to reach this?

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Hi, Operating on the names of variables is not possible in general; you can use reflection to operate on the members (data fields, properties, methods, ...) of an object. But that is somewhat advanced stuff, and not necessary for what you need. I understand you will get the paths one by one from somewhere (app.config), and want to perform some operations on them; hence I suggest you collect them in a Collection, probably a List< string> is appropriate. You might first check the file existence, and only then insert the path name in the list. Or you could insert the images themselves in the list; that would take more memory, but might better suits your needs. Or you could create a little class that describes an image, holding its path name, its image (initially null, until it is required, hence acting like a cache), and possibly some metadata. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        Love, happiness and fewer bugs for 2009!


        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups