Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Naming variables

Status
Not open for further replies.

fustrated

Programmer
Jun 8, 2005
5
CA
i was wondering if this is possible in c#:

say if i have a string myString which holds the value 'str'.
now, is there any way i can make a variable of type int or anything that has a variable name that is the value of myString?

(e.g string myString = "str" then
int str;
)
 
Not like that -- you would need to go through the Code DOM and generate a new class that has the variable named like that. You can then generate an assembly from the DOM and compile it, then load it via the usual static methods on the Assembly object.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
What context are you using this in?

Looks like a good design pattern could make things a lot easier...
 
ok,

i have several buttons
each button is asscoiated with a panel and they have the same names except for the prefix which start with either btn*** or pnl***.

once a button is clicked, i would like them to goto one method.

so, one method that accepts a button and a panel and there are several buttons and panels.
 
for (int i = 0 ; i < 10; i++)
{
Button temp = new Button();
temp.Click += new EventHandler(this.doclick);
this.myPanel.Controls.Add(temp);
}

10 buttons will be created in the panel. When you click on any one of them, they will all call the same method (doclick)

You then use the sender variable to determine which button was clicked.

Hope that helps.
 
Unless you are working in a really primitive language where you don't have any choice, using a design where one variable contains the name of another is almost always wrong. Even in perl, where almost any kind of linguistic abuse is not only tolerated but actively encouraged, it is listed in the documentation as "a really bad idea". It mostly comes from interpreted scripting languages where there is some function (usually EVAL) that re-interpolates the variable.

JurkMonkey's solution is much neater.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top