tags:

views:

405

answers:

6

I've inherited a code base and I'm writing a little tool to update a database for it. The code uses a data access layer like SubSonic (but it is home-grown). There are many properties of an object, like "id", "templateFROM" and "templateTO", but there are 50 of them.

On screen, I can't display all 50 properties each in their own textbox for data entry, so I have a listbox of all the possible properties, and one textbox for editing. When they choose a property in the listbox, I fill the textbox with the value that property corresponds to. Then I need to update the property after they are done editing.

Right now I'm using 2 huge switch case statements. This seems stupid to me. Is there a way to dynamically tell C# what property I want to set or get? Maybe like:

entObj."templateFROM" = _sVal;

??

+5  A: 

You need to use System.Reflection for that task.

entObj.GetType().GetProperty("templateFROM").SetValue(entObj, _sVal, null);

This should help you.

Dmitriy Matveev
Yeah, there's NO WAY I would have guessed that syntax. Thanks so much!
Matt Dawdy
+2  A: 

What you want is called reflection.

Thomas
+1  A: 
PropertyInfo[] properties = typeof(YourClass).GetProperties(BindingFlags.Instance | BindingFlags.Public)

you can bind this to your drop down list, and later on:

PropertyInfo property = typeof(YourClass).GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public)
property.SetValue(class, textBox.Text, null);
Rex M
+2  A: 

I think what you're looking for is Reflection. Here's a little snippet:

Type t = entObj.GetType();
t.GetProperty("templateFROM").SetValue(entObj, "new value", null);

On more of a usability note (and less of an answering-your-question note), you might want to look into using a PropertyGrid control. That listbox/textbox sounds like it could be pretty tedious to use.

Mike Powell
Excellent suggestion on the PropertyGrid control -- however, the stuff we are editing is multiline, and may not quite work very well. But you are right, this will get tedious. Hopefully I'll get a brainflash. Oh well.
Matt Dawdy
+1 for the PropertyGrid info. Very interesting!
Juan Calero
A: 

This sample helpful

public class aa
{
    private string myVar;

    public string value
    {
        get { return myVar; }
        set { myVar = value; }
    }   
}



private void button1_Click(object sender, EventArgs e)
    {
        aa a1 = new aa();
        System.Reflection.PropertyInfo pt = typeof(aa).GetProperty("value");
        pt.SetValue(a1, "hi",null);
        this.Text = a1.value;
    }
ebattulga
+1  A: 

On a related note, you're users will hate this interface if they need to update a lot of properties at once. Can you divide the properties into groups or pages that the user can move through more quickly?

Joel Coehoorn
I agree, they will hate it. However, we are trying to get a quick and dirty demo out the door. We have about 30 of these fields, always the same fields, and they are all fields where the user will enter a pattern (including carriage returns, hence multiline) to match on.
Matt Dawdy
A scrollable grid would also work. Then it could still be data driven, allow multi-line input, and not require your users to do as much navigation.
Joel Coehoorn