Answered by:
Dynamically declaring variable

Question
-
Hi,
I was wondering if there is any way to dynamically declare variables using the value of a string.....
I was thinking something like
// Declare Variables int VariablestoCreate; int counter = 0; String VariableBaseName = "RandomVariable"; String VariableName; VariablestoCreate = 10; // This value would change from user to user (which is why I need to dynamic create variables) for (int i = 0; i < 10; i++) { counter = counter + 1; VariableName = VariableBaseName + Convert.ToString(counter); // and here is where I would want to declare variables dynamically. Lets just say an int here. // so I was thinking something on the lines of int VariableName.Value(); // So as the loop continues you would end up with ten variables called RandomVariable1, RandomVariable2, ect }
The reason that I am interested in this is because I wish to dynamically add items (textbox, labels, ect) to my WFA - the number of which would change from user to user.
Is this possible, or am I going to have to think of another way around this?
Thanks
Answers
-
Hi,
Why can t you use HashTable or List or Dictionary to store those values, which will reduce the complexity.
Arrays, Lists, Dictionaries allows you to add any number of values. No Need to specify length, which ideally
suites in your case. If there is any specific reason that you want to declare variables only??
Please mark the post as answer if it is helpfull to you - Hiran Repakula- Proposed as answer by Derek Smyth Wednesday, March 9, 2011 8:41 AM
- Marked as answer by Leo Liu - MSFT Tuesday, March 15, 2011 2:23 AM
All replies
-
Hi Stanton,
There are several links I think helpful to you, please check if it helps.
C# Generate Random String of a Specific Length
How to generate a random string in c# 2.0
May this helps~
Ouch Liu
Welcome to visit by blog: Ouch@點部落 -
Hi,
Why can t you use HashTable or List or Dictionary to store those values, which will reduce the complexity.
Arrays, Lists, Dictionaries allows you to add any number of values. No Need to specify length, which ideally
suites in your case. If there is any specific reason that you want to declare variables only??
Please mark the post as answer if it is helpfull to you - Hiran Repakula- Proposed as answer by Derek Smyth Wednesday, March 9, 2011 8:41 AM
- Marked as answer by Leo Liu - MSFT Tuesday, March 15, 2011 2:23 AM
-
Hi Thomas,
You can use a Generic collection to collect the variables, to something like this:
List<String> VariableNames = new List<string>();
for (int i = 0; i < VariablestoCreate; i++)
{
counter = counter + 1;
VariableName = VariableBaseName + Convert.ToString(counter);
VariableNames.Add(VariableName);
}
foreach(Control c in Controls )
if (c.Name.Equals("textBox2"))
c.Text = VariableNames[1];
-
Hi,
Why can t you use HashTable or List or Dictionary to store those values, which will reduce the complexity.
Arrays, Lists, Dictionaries allows you to add any number of values. No Need to specify length, which ideally
suites in your case. If there is any specific reason that you want to declare variables only??
Please mark the post as answer if it is helpfull to you - Hiran Repakula
You want to follow this advice.
"The programmer, like the poet, works only slightly removed from pure thought-stuff. He builds his castles in the air, from air, creating by exertion of the imagination." - Fred Brooks