Dictionaries or variable name stored in another variable


During my high school/university I tried a bit of HTML/PHP. Topic of my BE thesis was more or less this: “On-line journey planer written in PHP with use of MySQL database.” It was not a rocket science at all. I just used a slice of map of my hometown and (let say) manually converted it to a graph. I placed a node on each intersection and link between nodes represented streets from the map. Coordinates of nodes and links between them were stored in two tables. That allowed me to use Dijkstra’s algorithm to calculate the shortest path between selected nodes. From that time I remember that PHP allowed user to store variable name in a variable, as code below illustrates:

The above code would result with displaying value of $nested_variable. I am not sure if this is still valid for current version of PHP but it worked in PHP4.x.

So the feature presented above might be quite useful. But C# doesn’t give that possibility. However, there is a mechanism that can be utilised to achieve the same goal – dictionary. It can be compared to a 1D table but its advantage is that you can use strings as an index. So, you store your variable name in another variable and then you refer to your dictionary with this variable as an index. Example is below:

The solution above is a bit more complicated than PHP, but I think if you use it to a bigger scope of variables it’s organised in a much more orderly manner.

So that’s it for today. For those of you who master Polish language I give a link to my thesis mentioned at the beginning of this post. Enjoy!

BE paper (*.PDF)