2

C# in VS2008, .NET 2.0, Winforms app.

I've created a small class whose constructor takes a reference to an object (which consists of lots of various types of data fields) and a "key" string which contains the name of one of the string members within the referenced object that is to be updated.

In this small class I've got a switch() statement that examines each possible legal value of the "key" string (which could really be an enum type, but whatever) and right there I'm trying to assign a single separate internal string variable to point to the appropriate string variable in the referenced object. Later on, other code updates this single separate internal variable, and I need the appropriate variable in the referenced object to be updated to match.

In C, I would have written something like:

char *p;
if (strcmp(pszKeyName, "FieldOne") == 0) {
    p = struct.string1;
} else if (strcmp(pszKeyName, "FieldTwo") == 0) {
    p = struct.string2;
} else if...

(Forgive the syntax; never was much good at pointers.) The idea is I could then just strcpy() something into 'p' and voila, the appropriate string variable (string1, string2, etc.) in the structure would be updated to match.

How can I accomplish this in C# .NET 2.0?

It's otherwise very simple code and I'm not looking to subclass, override, derive, or reinvent anything. Basically it's a simple class that represents a single text question and a pair of Yes/No Checkboxes. It's used in a Winforms TabControl/TabPage to display a Label with some predetermined text and let the user click either "yes" or "no" to answer the given question text. The class represents just one group of Label/CheckBox/CheckBox controls; there are a List<> of them created by calling code. (Yes, I would use a custom control to encapsulate the three, but the vertical height of the Label's text and hence, its location, is unknown at design time. That still might be worth looking into, though.)

Anyway at runtime, calling code figures out how many of these questions will be needed based on other settings in that same large referenced object, and it instantiates this little class several times as needed. I've got all the on-screen positioning figured out and even have the ..._CheckedChanged() handlers firing off when the CheckBoxes are checked.

The last remaining problem is that when my "yes" and "no" CheckBox handlers are called, they don't know which of several data members of the referenced object to write the word "yes" or "no" into.

1 Answer 1

1

Add a reference to System.Reflection and then try this:

public void SetStringValue(object o, String propertyName, String newValue)
{
    try
    {
        o.GetType().GetProperty(propertyName).SetValue(o, newValue, null);
    }
    catch (Exception ex)
    {
        throw new Exception(String.Format("Failure setting property {0}!", propertyName), ex);
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

This answer may be correct, but having to use reflection for such a simple scenario is a sign of underlying design issues
Why not just: o.GetType().GetProperty(propertyName).SetValue(o, newValue, null); ?
Thanks, but it doesn't seem to work. "o.GetType()" works and shows me the Name and Fullname of the class of the "o" object, but calling "o.GetType().GetProperty("strCancer")" returns null. I can't then call SetValue() against that null value. ("strCancer" is the name of the variable where I'm trying to store the "yes" or "no" corresponding to the two CheckBoxes.) These are new methods to me, so I'm going to start digging from here.
Sorry you beat me to it. I hit [Enter] while typing my comment. Can't do that in the comment box here; it thought I was done typing! I need to access the actual object "o" points to, not its type, I think. Yes, propertyName does indicate the name of the string data member I want to update.
Yes! I'm passing a reference to "o" currently, but could pass the actual object itself if that would work.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.