c# update instance property from variable parameter

Say I have this class:

class block999
{
 string parm1;
 string parm2;
 string parm3;
 string parm4;
 string parm5;

 block999(){}

 setValue(object sender, RoutedEventArgs e)
 {
 }
}

And a form with this code:

block999 B999 = new block999();    

TextBox parm1 = new TextBox();
TextBox parm2 = new TextBox();
TextBox parm3 = new TextBox();
TextBox parm4 = new TextBox();
TextBox parm5 = new TextBox();

parm1.LostFocus += new RoutedEventHandler(B999.setValue);
parm2.LostFocus += new RoutedEventHandler(B999.setValue);
parm3.LostFocus += new RoutedEventHandler(B999.setValue);
parm4.LostFocus += new RoutedEventHandler(B999.setValue);
parm5.LostFocus += new RoutedEventHandler(B999.setValue);

How can I set the correct property in the setValue method assuming that the instance property name == the textboxes' name?

Answers


Since you are using RoutedEventArgs I assume you are using WPF (or maybe SL).

I think the best option would be to set up data bindings between the text boxes and fields of your class. If you don't want to do that, I would recommend just updating all of the fields whenever any of the text boxes changes.

There's no way to access the name of a local variable from code. So you probably can't get the text box names. If they are fields or properties on your class, you could theoretically do so using reflection, but I wouldn't recommend that as a solution.


Define what you mean by "the textboxes' name". The name of the variable it's (temporarily) assigned to is meaningless. You could use the Name property of the control, except in the sample you post, you never assign it. If you were, it would be just:

setValue(object sender, RoutedEventArgs e) 
 { 
       TextBox tb = sender as TextBox;
       switch(tb.Name)
       {
          case "parm1":  
           this.parm1 = .... 
          // etc

       }
 } 

Need Your Help

Bootstrap from Twitter IE7 bug

jquery html css twitter-bootstrap

I found a bug in a site that i have created in IE7.

ASP.NET: How can I properly redirect requests with 404 errors?

asp.net iis error-handling

I'd like my ASP.NET MVC application to redirect failed requests to matching action methods of a certain controller. This works fine on my development machine running Windows 7, but not on my produc...