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.