0

A lot of times i want to automate some statements by using string formatting, but unfortunately it doesn't work !

for example if i have this code :

Textbox1.Text = "";
Textbox2.Text = "";
Textbox3.Text = "";

how can i achieve the same results by doing something like this :

for ( int i = 1; i < 4; ++i )
    string.Format( "Textbox{0}.Text", i ) = "";

the code above is completely wrong, my goal was to make the question clear .

Update : basically referring to a variable with a string

Focus
  • 117
  • 9
  • 5
    Create an array/list of TextBoxes – EZI Mar 26 '15 at 20:49
  • 2
    Widows forms or asp.net? – stuartd Mar 26 '15 at 20:51
  • @stuartd : thanks for the reply, anything even if it's a console application – Focus Mar 26 '15 at 20:54
  • How about using Hashtables?!! – Dan Hunex Mar 26 '15 at 20:56
  • 1
    There are no controls in a console app. – Pierre-Luc Pineault Mar 26 '15 at 20:59
  • Nobody seems to understand the question. All he wants is to refer to a variable with a string. That string could be built with string.Format. As far as I know, it's not possible. But I don't know everything. Maybe some shenanigans with reflection would make it possible. I don't know. – Pluc Mar 26 '15 at 21:03
  • You've obfuscated for security too much - to tell us you want to mess with textboxes and then say it's a console application. Programming's all about automating - what do you want exactly and why can't you achieve it? – CindyH Mar 26 '15 at 21:04
  • BTW, for strings the assignments could be chained: `Textbox1.Text = Textbox2.Text = Textbox3.Text = "Some string";`. But if you have quite a few controls to work with then I'd go with a `List<>` and call `ForEach` – Setsu Mar 26 '15 at 21:05
  • @Pluc thank you Pluc, you described exactly what i want ! – Focus Mar 26 '15 at 21:06
  • @CindyH all i want is to refer to a variable with a string – Focus Mar 26 '15 at 21:07
  • You can access fields by using reflection, and check their names that way, but it would be awfully slow. – CindyH Mar 26 '15 at 21:08
  • @CindyH - fields, but not variables. So reflection will not help OP according to "i want is to refer to a variable with a string" comment. – Alexei Levenkov Mar 26 '15 at 21:10
  • If suggested duplicate not enough - this search http://www.bing.com/search?q=c%23+refer+to+variable+by+index will give you more options. – Alexei Levenkov Mar 26 '15 at 21:16
  • @AlexeiLevenkov Thank you, i'll search more . – Focus Mar 26 '15 at 21:18

2 Answers2

1

In asp.net, you can use the FindControl method of the parent control to these textboxes. Such as:

for ( int i = 1; i < 4; ++i )
    (Page.FindControl("Textbox" + i) as Textbox).Text = "";
Tyler Day
  • 1,690
  • 12
  • 12
  • great, what about if they were not textBoxes ? i mean something else not of type Control ! – Focus Mar 26 '15 at 20:55
  • Well everything in asp.net inherits from Control. If it's not a textbox then you can cast it to whatever you want. – Tyler Day Mar 26 '15 at 20:57
  • Is as operator better than a simple cast in this case? – D. Ben Knoble Mar 26 '15 at 20:59
  • 2
    Great solution. But when using the FindControl, remember that it's recursive - you have to start with the outermost control, then loop through all of its children, etc. If there's a textbox inside a panel, it won't be found when you just look at the controls on the window. But you can do great stuff with a recursive control formatter - I have in the past but no longer have access to the code. – CindyH Mar 26 '15 at 21:00
  • @CindyH - Yes, good to point this out. Usually I put my controls in a parent panel. Then I do Panel1.FindControl("txt1") etc. if you want to skip the recursive route. – Tyler Day Mar 26 '15 at 21:02
  • @Ben - "as" vs a cast would be about the same in this situation. "as" will return null if the cast operation fails, rather than raising an exception. – Tyler Day Mar 26 '15 at 21:05
  • Ok. I wasnt aware of the difference; thanks. – D. Ben Knoble Mar 26 '15 at 21:06
  • For WinForms, see [this answer](http://stackoverflow.com/a/4483962/1378739) – Setsu Mar 26 '15 at 21:09
-3

You can use dynamic:

    class Whatever { public string Text { get; set; } }
    class It { public string Text { get; set; } }
    class Can { public string Text { get; set; } }
    class Be { public string Text { get; set; } }

    static void Main()
    {
        var whatever = new Whatever();
        var it = new It();
        var can = new Can();
        var be = new Be();

        foreach (var item in new dynamic[] {whatever, it, can, be})
            item.Text = item.ToString();
    }

But you should enumerate processing items in any case and know property/method names in the compile time. Otherwise, go ahead and use reflection.

ie.
  • 5,982
  • 1
  • 29
  • 44