0

I have a multiple textboxes in a form, and with the typed information I create an object called UserClass. I want to create multiple users and for this I have different textboxes called tbName1, tbName2 etc. Is it possible to use a variable in the textboxname? E.G. tbName[variable].Text

newUsers.Add(new ClassLibrary.UserClass
(
    "AAAAAAAA",
    tbName[variable].Text,    //Is it possible to do something like this?
    " ",
    tbLastname[variable].Text,
    tbEmail[variable].Text,
    " ",
    "0497111111",
    "0611111111",
    "USER"
));
Loofer
  • 6,841
  • 9
  • 61
  • 102
Aecnoril
  • 11
  • 1
  • 3
    It is not possible since variables names has to be defined at compile time. – Andrey Korneyev Apr 10 '15 at 13:28
  • 3
    No, store your textboxes in an array (or dictionary) and then you can refer to them that way. – Jamiec Apr 10 '15 at 13:30
  • 1
    You actually can do this even at run time because of C# vastly available metadata in each object. Check out this link: http://stackoverflow.com/questions/13578930/what-function-to-use-in-getting-all-textbox-names-in-form-using-c just use linq – NKamrath Apr 10 '15 at 13:30
  • @NKamrath That works for web forms because the designer creates class members with the same name as the control name - _in general_ there's no way to dynamically select a variable at run time. You can find _class members_ through reflection but not variables. – D Stanley Apr 10 '15 at 13:46
  • If you want to enter data for multiple users maybe consider using a data grid instead of many textboxes? – PiotrWolkowski Apr 10 '15 at 13:48
  • @DStanley , you are correct, I should have clarified that I was simply referring to the fact that this functionality is possible because you can come up with a scheme to use its name. So you can achieve what the question is asking functionally to a degree, but not by getting variable names themselves (as you stated), but by getting the names of the text boxes. Not an exact answer, but it is functionally similar with minor code changes if I understand the question correctly – NKamrath Apr 10 '15 at 13:53

2 Answers2

0

If understand your requirements correctly, you have a variable called "variable", which is of type Integer and depending of the value of this variable you want to get the Text property of different textboxes which are named after the same schema, but with incrementing integer-suffix. You could solve this via reflection, like this:

var tbNameX = (TextBox)this.GetType().GetProperty(tbName + variable).GetValue(this, null);
var tbLastnameX = (TextBox)this.GetType().GetProperty(tbLastname + variable).GetValue(this, null);
var tbEmailX = (TextBox)this.GetType().GetProperty(tbEmail + variable).GetValue(this, null);

newUsers.Add(new ClassLibrary.UserClass
(
    "AAAAAAAA",
    tbNameX.Text,    //Is it possible to do something like this?
    " ",
    tbNameX.Text,
    tbEmailX.Text,
    " ",
    "0497111111",
    "0611111111",
    "USER"
));

This requires a very strict naming on your textboxes, what makes it very vulnerable to mistakes. I don't recommend this for production use, but it should work.

Marvin
  • 329
  • 1
  • 6
0

I'm afraid you won't be able to declare a variable on runtime as all variables have to be declared before compilation.

92AlanC
  • 1,327
  • 2
  • 14
  • 33