2

Does .NET natively support anything similar to PHP's variable variables?

If not, how1 could such a feature be most easily implemented?


1 If you think variable variables are always bad, feel free to state your case but the main question is: how can they be implemented?

Dinah
  • 52,922
  • 30
  • 133
  • 149
  • I really don't think someone could call variable variables bad. Using User Input to access variable variables might be though. – Tyler Carter Aug 28 '09 at 15:16

4 Answers4

6

Why not just use a Dictionary ?

Dictionary<string,string> stuffHash = new Dictionary<string,string>();

string varname = "TheNameOfTheVar";
string value = "foo";

stuffHash[varname] = value;

No actual need to do this ugly thing.

Vinko Vrsalovic
  • 330,807
  • 53
  • 334
  • 373
3

.Net does not support "variable variables" natively - probably mainly because it is a [strongly typed language][1].

However, it does have support for dynamically creating instances of a type, at runtime, which could be used to accomplish similar behaviors as the PHP variable variables.

Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
1

No, none of the .NET languages support anything like this. This could be implemented by one of the compiler teams but I doubt they would ever do it.

As to how this could be implemented by you (not by the C# compiler team) would be to store all of your variable variables in a Dictionary<String,Object> - this would allow you to associate a string with an object.

I have never really understood what problem is solved by variable variables (in other words, I have never heard a good argument for needing to use them). I would be interested to see an example where they were needed as I would imagine it wouldn't be too hard to find a better approach to solving the problem without variable variables.

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • 1
    They are never *needed*. They just sometimes make things easier by having to type less. Not a great benefit for all the potential cost. – Vinko Vrsalovic Aug 28 '09 at 15:35
  • @VinkoVrsalovic: I'd call that assessment a matter of opinion. In PHP, I found variable variables to be a great benefit for creating a lightweight homespun MVC framework. Instead of creating another templating syntax for my view, I use PHP as the templating language for which it was originally intended. I'm sure there's plenty of legitimate criticism of my decision but it allowed me to create a light and very powerful MVC framework in no time. It's been very useful to me. – Dinah May 24 '12 at 00:32
  • @Dinah You are making my point. They are not needed, they can be convenient, as they've been for you. You can certainly (and almost as easily) create a PHP templating system in PHP without using them. – Vinko Vrsalovic May 24 '12 at 08:35
  • @VinkoVrsalovic: you're treading on dangerous ground when you argue about what's "needed." When viewed individually, there are very few language features or library components which are truly needed. On the issue of benefit vs. potential cost, I think we should agree to disagree. – Dinah May 24 '12 at 21:06
1

This is a feature deeply embedded in dynamic languages. C# has its roots as a static, object-oriented language, and up to C# 3.0 this means no luck accomplishing what you want in any proper way. However, C# 4.0/.NET 4.0 introduces the dynamic keyword, which allows variables to be dynamically typed, as in PHP. Unfortunately, although this is a leap forward in the path of C# becoming a static/dynamic hybrid language, it is missing the crucial eval function that almost every dynamic language has. With the rumoured compiler-as-a-service feature of C# 5.0/.NET 5.0, this will effectively be introduced (though the internal behaviour would not be the same). Until then, there's no decent solution short of the hack of using a Dictionary to store variable names.

Noldorin
  • 144,213
  • 56
  • 264
  • 302
  • I wouldn't call the use of a Dictionary a hack. I would call the $a = "foo"; $$a == $foo a hack. – Vinko Vrsalovic Aug 28 '09 at 15:49
  • In the context of trying to mimic dynamic languages, it's definitely a hack in my opinion. – Noldorin Aug 28 '09 at 15:52
  • What I'm trying to say is that using variable variables in a dynamic language is a hack at best and there are better solutions for that use case. – Vinko Vrsalovic Aug 28 '09 at 16:17
  • @Vinok: Indeed, their usage is often a hack even in dynamic languages. What I meant here was that the *implementation* was a hack. – Noldorin Aug 28 '09 at 16:31