Home > Software design >  How to change variable names in runtime
How to change variable names in runtime

Time:11-08

I have a loop that I need to create Entries dynamically, and with that, I need to setFocus() on a specifically Entry, and then, another one, etc.

How can I change the name of variables in runtime to I identify it?

for(i = 0; i < list.Count; i  )
{
  //the result that I want:
  Entry entry   i = new Entry(){ Placeholder = "Entry number"   i.ToString() };
}

//result
entry1 =  /* Placeholder */ "Entry number 1";
entry2 =  /* Placeholder */ "Entry number 2";
entry3 =  /* Placeholder */ "Entry number 3";

EDIT:

I forgot to put an Entry event that I need to use:

entries[i].Completed  = (s, e) =>
{
   if (!entries[i].Text.Contains("\u00b2"))
   {
    entries[i].Text  = "\u00b2";
   }
};
   entries[i].Focus();

when it enters in this event, it can't know how entry that I'm calling, always get the last entry of this array.

CodePudding user response:

You would use some kind of collection, for example an array:

var entries = new Entry[3];
for(i = 0; i < list.Count; i  )
{
  //the result that I want:
  entries[i] = new Entry(){ Placeholder = "Entry number"   i.ToString() };
}

//result
entries[0] =  /* Placeholder */ "Entry number 1";
entries[1] =  /* Placeholder */ "Entry number 2";
entries[2] =  /* Placeholder */ "Entry number 3";

Variable names mostly exist before compilation, so asking how to change it at runtime is kind of a nonsensical question.

CodePudding user response:

the s in the event parameters is the sender - that is, the object that fired the event

entries[i].Completed  = (s, e) =>
{
   var entry = (Entry)s;

   if (!entry.Text.Contains("\u00b2"))
   {
     entry.Text  = "\u00b2";
   }
};
  • Related