looping through variables whose names start with...
-
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 checkor, 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?
-
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 checkor, 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?
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?
-
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?
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!