Thoughts on how to get this dynamic variable name workaround to work?

M

Mark S.

As I understand it, C# doesn't offer dynamic variable names. Below is my
attempted workaround. Is what I'm doing possible?

FYI, I already read all the "why in the world do you need dynamic variabale
names" comments by other posters, if you are kind enough to post a reply
please either say it's impossible, or it is possible if I did x. TIA

using System;

namespace ConsoleForTesting
{
class test
{
public static string foo1 = "1";
public static string foo2 = "2";
public static string foo3 = "3";

public static string getFoo(int rowNumber)
{
switch (rowNumber)
{
case 1:
return foo1;
case 2:
return foo2;
case 3:
return foo3;
}
return "";
}

static void Main(string[] args)
{

Console.WriteLine(foo2);

// how can x be used to represent foo2?
// assume x can't be used later to update the foo2.
String x = getFoo(2);
x = "updated";

// foo returns 2, but I need it to return "updated"
Console.WriteLine(foo2);

Console.ReadLine();
}
}
}
 
M

Mark S.

This seems to work, but I wish I could get the other version to work. I
looked at pointers, except it requeres /unsafe...

using System;

namespace ConsoleForTesting
{
class test
{
public static string foo1 = "1";
public static string foo2 = "2";
public static string foo3 = "3";

static void Main(string[] args)
{
Console.WriteLine(foo2);

switch (2)
{
case 1:
changeString(ref foo1);
break;
case 2:
changeString(ref foo2);
break;
case 3:
changeString(ref foo3);
break;
}

Console.WriteLine(foo2);

Console.ReadLine();
}

static void changeString(ref String x)
{
x = "updated";
}
}
}
 
P

Peter Duniho

This seems to work, but I wish I could get the other version to work. I
looked at pointers, except it requeres /unsafe...

I'm not really clear on what it is you're trying to do. The two examples
you've provided don't really do the same thing, though they are similar.

In the former case, you appear to want a reference to a reference with a
lifetime outside a method. In the second case, you seem satisfied to have
a reference to a reference that exists only for the duration of a method.

Both are useful, but they aren't really the same thing.

For what it's worth, as far as I know there's no variable type that gives
you a reference to a reference in C#, except in the contexts you've
already identified (using pointers, and using the "ref" keyword..."out"
work similarly, of course).

Also for what it's worth, I wouldn't call this a "dynamic variable name".
When I read that, I got the impression you wanted a variable that has an
automatically generated name, or a name that can change over time.
Neither have anything to do with what you're trying to do.

Pete
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Mark said:
As I understand it, C# doesn't offer dynamic variable names. Below is my
attempted workaround. Is what I'm doing possible?

Everything is possible. Using reflection you can access any class
members, but I doubt that is what you want to do.

What are you trying to accomplish really? What do you intend to use this
for?

There is probably an object oriented way of doing what you want, but it
definitely doesn't involve dynamic variables in any way, so we have to
take a step back see it.
FYI, I already read all the "why in the world do you need dynamic variabale
names" comments by other posters, if you are kind enough to post a reply
please either say it's impossible, or it is possible if I did x. TIA

That is a common reaction to requests for dynamic variables. In
scripting languages you can use dynamic variables, but it's widely
misused for sloppy shortcuts where there are well behaved alternatives
that work just as well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top