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?

1
  • I really don't think someone could call variable variables bad. Using User Input to access variable variables might be though. Commented Aug 28, 2009 at 15:16

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Agree. Using a dictionary is going to be the easiest. Might want to declare it as Dictionary<string,object> and then cast the objects to whatever types you are using.
Yes <string,object> might be a better fit, depending on actual usage though.
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.

Comments

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.

4 Comments

I wouldn't call the use of a Dictionary a hack. I would call the $a = "foo"; $$a == $foo a hack.
In the context of trying to mimic dynamic languages, it's definitely a hack in my opinion.
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.
@Vinok: Indeed, their usage is often a hack even in dynamic languages. What I meant here was that the implementation was a hack.
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.

4 Comments

They are never needed. They just sometimes make things easier by having to type less. Not a great benefit for all the potential cost.
@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 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.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.