Dynamiclly naming variables
-
I have to name varibles depending on a
count
variable. I want to append it to the variable likeint "newno" + count = new int();
This gives a syntax error I also tried takin the new value in a string, but that didn't work as well .. It said : A local variable named 'var' is already defined in this scope :wtf: when I triedstring var = "newno" + count;
andint var = new int();
HelP!! _____________________________________________________ Yea! I could be wrong... -
I have to name varibles depending on a
count
variable. I want to append it to the variable likeint "newno" + count = new int();
This gives a syntax error I also tried takin the new value in a string, but that didn't work as well .. It said : A local variable named 'var' is already defined in this scope :wtf: when I triedstring var = "newno" + count;
andint var = new int();
HelP!! _____________________________________________________ Yea! I could be wrong...Hi! What exactly are you trying to do? Are you sure you shouldn't be using arrays of some sort instead? An example:
string[] varArray = new string[ sizeGoesHere ]; for( int i=0; i Zerxes
-
Hi! What exactly are you trying to do? Are you sure you shouldn't be using arrays of some sort instead? An example:
string[] varArray = new string[ sizeGoesHere ]; for( int i=0; i Zerxes
A little error snuck in..
for( int i=0; i< Zerxes
-
A little error snuck in..
for( int i=0; i< Zerxes
Ack.. That's what you get for trying to post code. This should be correct for( int i=0; i
-
Hi! What exactly are you trying to do? Are you sure you shouldn't be using arrays of some sort instead? An example:
string[] varArray = new string[ sizeGoesHere ]; for( int i=0; i Zerxes
ok but that's no what I'm tryin to do.. I have a variable
static int count = 0;
. it is global. I am adding a text box each time I click a button say..private void btnAddCond_Click(object sender, System.EventArgs e)
{
TextBox "tb" + count = new TextBox();
//..right here ..I need to make each textbox have a uniqe name as and when it is added..I can't do that!!:mad:
this.Controls.Add("tb" + count); // I want this to work..!!:mad:
}I hope it explains my plight!!:(( Help PLZ _____________________________________________________ Yea! I could be wrong...
-
ok but that's no what I'm tryin to do.. I have a variable
static int count = 0;
. it is global. I am adding a text box each time I click a button say..private void btnAddCond_Click(object sender, System.EventArgs e)
{
TextBox "tb" + count = new TextBox();
//..right here ..I need to make each textbox have a uniqe name as and when it is added..I can't do that!!:mad:
this.Controls.Add("tb" + count); // I want this to work..!!:mad:
}I hope it explains my plight!!:(( Help PLZ _____________________________________________________ Yea! I could be wrong...
Thought about using the
Name
property of TextBox class? Otherwise i don't see a need to give your variable a unique name, because it's gone as soon as you leave the event handler.private void btnAddCond_Click(object sender, System.EventArgs e)
{
TextBox textbox1 = new TextBox();
this.Controls.Add(textbox1);
}
// identifier textbox1 is out of scope
-
Thought about using the
Name
property of TextBox class? Otherwise i don't see a need to give your variable a unique name, because it's gone as soon as you leave the event handler.private void btnAddCond_Click(object sender, System.EventArgs e)
{
TextBox textbox1 = new TextBox();
this.Controls.Add(textbox1);
}
// identifier textbox1 is out of scope
-
I have to name varibles depending on a
count
variable. I want to append it to the variable likeint "newno" + count = new int();
This gives a syntax error I also tried takin the new value in a string, but that didn't work as well .. It said : A local variable named 'var' is already defined in this scope :wtf: when I triedstring var = "newno" + count;
andint var = new int();
HelP!! _____________________________________________________ Yea! I could be wrong... -
as Mr. Stefan Troschütz has stated for TextBoxes , How do you do it for a simple Integer? _____________________________________________________ Yea! I could be wrong...
What exactly do you want to do with your integers? I guess storing them in some way like the textboxes :confused: Take a look at the Collections namespace[^] which contains interfaces and classes that define various collections of objects, such as lists, queues, bit arrays, hashtables and dictionaries.
-
What exactly do you want to do with your integers? I guess storing them in some way like the textboxes :confused: Take a look at the Collections namespace[^] which contains interfaces and classes that define various collections of objects, such as lists, queues, bit arrays, hashtables and dictionaries.
I want to name anything (integers, long, double, char etc ) which does not have a name property Dynamically. ex.
static int count = 0; // global
//......
{
//..somewhere in the code
int "var" + count = new int() //you get an error on this.:eek:
}do you get what I'm tryin' to say?? _____________________________________________________ Yea! I could be wrong...
-
I want to name anything (integers, long, double, char etc ) which does not have a name property Dynamically. ex.
static int count = 0; // global
//......
{
//..somewhere in the code
int "var" + count = new int() //you get an error on this.:eek:
}do you get what I'm tryin' to say?? _____________________________________________________ Yea! I could be wrong...
Yeah, I get but sorry this saud_a_k wrote: int "var" + count = new int() //you get an error on this isn't possible. To accomplish a similar effect you could use a SortedList[^] to store your integers whereby the key values are created by the term
"var" + count.ToString()
.
-
I have to name varibles depending on a
count
variable. I want to append it to the variable likeint "newno" + count = new int();
This gives a syntax error I also tried takin the new value in a string, but that didn't work as well .. It said : A local variable named 'var' is already defined in this scope :wtf: when I triedstring var = "newno" + count;
andint var = new int();
HelP!! _____________________________________________________ Yea! I could be wrong...You could use an
IDictionary
implementation like so:Hashtable vars = new Hashtable();
for (int i=0; i< 10; i++)
vars["newno" + i.ToString()] = i;Once your code is compiled, it cannot be changed. You either need to use Reflection Emit or the CodeDom to generate new source and compile it on the fly (Reflection Emit actually emits an assembly - no compilation necessary but it's far more complex and requires that you know and understand IL). For your compiled code, you can only maintain the appearance of dynamic variables. This is done through collections, dictionaries, or the ComponentModel (probably not the best idea in your case). Using the approach I described above would probably be the easiest way. To get the
int
, then, just doint i = (int)vars["newno0"];
.Microsoft MVP, Visual C# My Articles