charlie Posted September 11, 2004 Posted September 11, 2004 Hi all! Is there any way for calling variables as strings? I mean, if I have two Labels (label1 and label2, I want to call'em with something like: for (int i=1; i<3; i++) { whatever("label" + i).Text = "C# forum - Label " + i; } Thanks a lot! :) Quote
Administrators PlausiblyDamp Posted September 12, 2004 Administrators Posted September 12, 2004 You could use an array of labels and loop through them or alternatively loop through the Form's Controls collection and compare the control name to the string. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Gladimir Posted September 12, 2004 Posted September 12, 2004 Hi all! Is there any way for calling variables as strings? I mean, if I have two Labels (label1 and label2, I want to call'em with something like: for (int i=1; i<3; i++) { whatever("label" + i).Text = "C# forum - Label " + i; } Thanks a lot! :)Hi Charlie, As PlausiblyDamp mentioned, it is trivial to loop through all the controls of a form and identify them by type or name. Here is some code: [color=Blue]int[/color] labelCount = 0; [color=Blue]for[/color] ([color=Blue]int[/color] i = 0; i < [color=Blue]this[/color].Controls.Count; i++) [color=Blue]if[/color] ([color=Blue]this[/color].Controls[i].GetType().ToString().EndsWith("Label")) labelCount++; label1.Text = labelCount.ToString(); I used Label1 just to give me a label to count. I hope this helps. Quote Never ascribe to malice that which is adequately explained by incompetence. - Napoleon Bonaparte
charlie Posted September 12, 2004 Author Posted September 12, 2004 Thanks! That's what I wanted! :) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.