473,500 Members | 1,605 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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();
}
}
}
Mar 31 '07 #1
3 1935
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";
}
}
}
Mar 31 '07 #2
On Sat, 31 Mar 2007 07:40:58 -0700, Mark S. <ma***@yahoo.comwrote:
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
Mar 31 '07 #3
Mark S. wrote:
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.

--
Göran Andersson
_____
http://www.guffa.com
Mar 31 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

39
3121
by: Marco Aschwanden | last post by:
Hi I don't have to talk about the beauty of Python and its clear and readable syntax... but there are a few things that striked me while learning Python. I have collected those thoughts. I am...
7
3401
by: Jack | last post by:
Hi, I am trying to test a sql statement in Access which gives me the error as stated in the heading. The sql statement is built as a part of asp login verification, where the userid and password...
1
17626
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
2
2923
by: deejayquai | last post by:
Hi I'm trying to produce a report based on a dynamic crosstab. Ultimately i'd like the report to actually become a sub report within a student end of year record of achievement. The dynamic...
35
1848
by: Justin Weinberg | last post by:
My thoughts on this.... http://msdn.microsoft.com/vbasic/Future/default.aspx?pull=/library/en-us/dnvs05/html/vb9overview.asp My thoughts: 1. Regarding Implicit types, I don't use type...
4
3674
by: ImOk | last post by:
I come from the Visual Foxpro world, which is one reason I love PHP. VFP is a scripting type language with macro substitution abilities similar to PHP. Besides the regular expansion I can do...
7
10704
by: Joey | last post by:
I don't care what .net language this come in but I really need to determine if a disk is dynamic or basic. I have posted something in the WMI group but no one knows how to do it. Does anyone have...
0
5079
by: mix01 | last post by:
Hi, I am trying to get some VBA code working, but am preplex as to why it does not work. I would really appreciate any level of help. Many thanks, Mix01 Version of the program
2
5479
by: makennedy | last post by:
Hi Experts, Please help, I am a newbie to ASP.NET 2.0 may be I am doing something wrong or there may be a bug somewhere. Basically I have a TreeView Control which I have created...
0
7014
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7229
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7395
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5485
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4921
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3108
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.