Variable variables

sassur

Member
Joined
Nov 26, 2003
Messages
5
In PHP you can do this

PHP:
$name = "Fredrik";

$var = "name";

echo ${$var};

and the output will be

Code:
Fredrik

Now I wonder if you can do somethins similar in C# or VB.NET?

---
Sassur
 
Originally posted by Bodybag
dim s as string= "John"

response.write (s)

For a starter thats classic VB I asked about C# or VB.NET.

And thats not what I asked for.

I know how to define a variable and print it to the page.
 
Originally posted by samsmithnz
Its almost exactly the same in C#.

Code:
string s = "John";

response.write (s);

But as I said i the previous post, that isnt what Im asking for.

Trying to explain better:
PHP:
$name1 = "Fredrik";
$name2 = "Andreas";

$var = "name1";
echo $var . " = " . ${$var};
echo "<br>";

$var = "name2";
echo $var . " = " . ${$var};
echo "<br>";

and the output will be

Code:
name1 = Fredrik
name2 = Andreas
 
dim s1 as string = "jack"
dim s2 as string = "Daniels"

label1.text = "i drink " & s1 & " " & s2 & "<br>"
 
It looks more complicated then it is. The key to it is the variable $var

I will try to explane each line by it self

PHP:
$name1 = "Fredrik";
initiates $name1 to Fredrik

PHP:
$name2 = "Andreas";
initiates $name2 to Andreas

PHP:
$var = "name1";
initiates $var to name1

PHP:
echo $var . " = " . ${$var};
this will print :
name1 = Fredrik

take a look at ${$var}, that code means:
the variable with the name in variable $var
therefore
${$var} equels the variable $name1

PHP:
echo "<br>";
prints "<br>"

PHP:
$var = "name2";
set $var to name2

PHP:
echo $var . " = " . ${$var};
name1 = Andreas
now the variable $var conatins name2
therefore
${$var} equels the variable $name2

PHP:
echo "<br>";
prints "<br>"

Hoppes this makes it a little clearer.
 
Not sure if you can do it exactly the way you say but you could achieve something similar with a collection.

Code:
		Dim vars As New Collections.Specialized.StringDictionary

		vars.Add("Name1", "Frederik")
		vars.Add("Name2", "Andreas")

		Dim name As String = "Name1"
		Response.writeline(vars(name)
 
Originally posted by PlausiblyDamp
Not sure if you can do it exactly the way you say but you could achieve something similar with a collection.

Code:
		Dim vars As New Collections.Specialized.StringDictionary

		vars.Add("Name1", "Frederik")
		vars.Add("Name2", "Andreas")

		Dim name As String = "Name1"
		Response.writeline(vars(name)

Thanks, but not exactly that.

It seams that you cant use the Eval function to achieve what I want, but i haven
 
I dont see the use for what youre asking, seems to me like more memory consumption, if I want to copy the value of 1 variable into another variable Ill do something really simple
C#:
string name1 = "Superman";
string name2 = "Batman";

name1 = name2;
There you go, I assigned the value of the second variable into the first one, whats the result ? "BATMAN", why do things the hard way?????, this is exactly the same thing youre doing
 
Originally posted by iebidan
I dont see the use for what youre asking, seems to me like more memory consumption, if I want to copy the value of 1 variable into another variable Ill do something really simple
C#:
string name1 = "Superman";
string name2 = "Batman";

name1 = name2;
There you go, I assigned the value of the second variable into the first one, whats the result ? "BATMAN", why do things the hard way?????, this is exactly the same thing youre doing

I think hes trying to print the actual name of the variable too. Thats the main difference.
 
Back
Top