Reputation: 3585
Is there a way to store a reference to a property into a variable that will allow me to access it just as if I were accessing that objects property, like so:
var ReferenceVariable = Object.Property;
ReferenceVariable = "SOMETHING";
If (Object.Property == "SOMETHING")
//It worked! Yaay!
If so, how do I go about doing that?
EDIT: For a bit of clarity, this is what is going on:
private void UpdateColor(){
if (radioButton1.Checked){
Object.Color1 = Color.Red
}
if (radioButton2.Checked){
Object.Color2 = Color.Blue
}
.
.
.
if (radioButtonN.Checked){
Object.ColorN = Color.ColorN
}
}
This is very sub-optimal. Ideally the issue would be handled in the function that fires when the radio button is changed, so that it would be something like...
private void RadioButton_CheckChanged(object sender, eventargs e){
//Something is done here to tell the program that we are interested in Object.Color...Whatever
}
private void UpdateColor(){
//Now we know what color we're looking at, we can just do it in one step rather than looking at a thousand (I exaggerate of course) radio buttons checked states.
}
I hope that helps you help me a little bit more...
Upvotes: 3
Views: 4313
Reputation: 3084
Assuming I'm understanding your need correctly, you could use an Action<>
delegate to set your properties. The following example code (in a Windows Forms app) uses a Dictionary
to store a delegate for each radio button. The radio buttons all share the same event handler, which retrieves the delegate from the dictionary and sets it as the current delegate in the colorSetter
variable. I'm just using some buttons on the form to change color, and depending on which radio button is checked, the appropriate color property will be changed.
public partial class Form1 : Form {
private readonly ColorPropertyObject cpo = new ColorPropertyObject();
private Action<Color> colorSetter;
private readonly Dictionary<RadioButton, Action<Color>> setterDictionary =
new Dictionary<RadioButton, Action<Color>>();
public Form1() {
InitializeComponent();
setterDictionary.Add(radioButton1, c => cpo.Color1 = c);
setterDictionary.Add(radioButton2, c => cpo.Color2 = c);
setterDictionary.Add(radioButton3, c => cpo.Color3 = c);
}
private void radioButton1_CheckedChanged(object sender, EventArgs e) {
colorSetter = setterDictionary[(RadioButton)sender];
}
private void button1_Click(object sender, EventArgs e) {
colorSetter(Color.Blue);
}
private void button2_Click(object sender, EventArgs e) {
colorSetter(Color.Black);
}
private void button3_Click(object sender, EventArgs e) {
colorSetter(Color.Red);
}
private void button4_Click(object sender, EventArgs e) {
Console.WriteLine(cpo.Color1 + " - " + cpo.Color2 + " - " + cpo.Color3);
}
}
public class ColorPropertyObject {
public Color Color1 { get; set; }
public Color Color2 { get; set; }
public Color Color3 { get; set; }
}
Upvotes: 2
Reputation: 126864
Quick answer to the question as asked:
Is there a way to store a reference to a property into a variable that will allow me to access it just as if I were accessing that objects property?
C# is not a language that supports passing and holding references to properties. Some languages might support that, but C# is not one of them. You can certainly reference the same object that is referenced by a property and make any supported mutations to its held state, but if want to later overwrite the property value completely (basically: reassignment, as shown in your example), you will need to keep a reference to the object that owns the property.
Now to perhaps help solve your problem.
You might explore the idea of changing strategies based upon the user input. In its simplest form where you are maybe just changing colors, then this pattern might not get you much. But if you are instead changing colors, perhaps writing some custom text, and firing lasers at unsuspecting rubber ants, then that higher degree of complexity can be encapsulated by classes that contain those strategies, and you can simply toggle between them (or activate one) based on the selection.
These strategies could be expressed as classes that conform to a given interface, or they could simply be delegates. You can decide on your own what makes most sense for your particular project.
To be clear, in your radio button event handler, you identify which strategy you wish to employ. This strategy knows what to do with your objects.
myStrategy = redStrategy; // Func, Action, class instance as applicable
If your strategy were simply expresseds as Action<...>
, it might be something like
Action redStrategy = () =>
{
obj.Color1 = Color.Red;
obj.Text1 = "Red selected";
obj.BurnRedRubberAnts();
}
In your update method, you simply invoke the pre-identified strategy with any applicable argument (none defined above) and let it execute.
myStrategy(/* args */)
You could alter this to accept arguments, return values (use Func
instead of Action
), or to be fully developed and testable classes. Or all of this could prove to be overkill for what you want to do, in which case you can just use that switch statement you were trying to avoid.
If you are interested in techniques such as this, I suggest perhaps reading about the Strategy Pattern, and the Command Pattern also appears to be emerging.
Upvotes: 0
Reputation: 127
static void Main(string[] args)
{
var props = typeof(MyClass).GetProperties();
foreach (var propertyInfo in props)
{
if (propertyInfo.Name == "property")
{
var prop = propertyInfo;
}
}
}
}
class MyClass
{
public static string property { get; set; }
}
Upvotes: 0