I need to pass a property to a function...as a property rather than a value

Trips

Well-known member
Joined
Aug 7, 2010
Messages
2,788
I have some C# code using the following properties:
<div style="background-color:white; color:black
<pre style="border:solid 1px black <span style="color:blue class A
{
<span style="color:blue public <span style="color:blue string StringProp
{
<span style="color:blue get
{
<span style="color:green // return something
}
<span style="color:blue set
{
<span style="color:green // Save the new value
}
}
<span style="color:blue public <span style="color:blue bool BoolProp
{
<span style="color:blue get
{
<span style="color:green // return something
}
<span style="color:blue set
{
<span style="color:green // Save the new value
}
}

[/code]

I need to do something like this in a function:
<div style="background-color:white; color:black
<pre style="border:solid 1px black <span style="color:blue class B
{
A a;

System.Windows.Forms.TextBox textBox = <span style="color:blue new System.Windows.Forms.TextBox();
System.Windows.Forms.CheckBox checkBox = <span style="color:blue new System.Windows.Forms.CheckBox();

<span style="color:blue void SomeMethod()
{
<span style="color:blue if(a.StringProp != textBox.Text)
a.StringProp = textBox.Text;

<span style="color:blue if(a.BoolProp != checkBox.Checked)
a.BoolProp = checkBox.Checked;

<span style="color:green // Repeat the above a few dozen times with different controls and properties
}
}
[/code]

Notice how repetive that is. I need to move that repetive code into a function. I figure a generic would help deal with the different types. But I have to somehow pass a property. I dont want to pass the return value of the property,
but rather I need the function to call the get and set accessors of the property for me. The code below works (in theory) for the string, but not the bool. The problem is that the string is a reference type and the bool isnt. The function
works only for reference types.
<div style="background-color:white; color:black
<pre style="border:solid 1px black <span style="color:blue void CopyIfNoMatch<type>(type field, type CtrlValue)
{
<span style="color:blue if(!field.Equals(CtrlValue))
field = CtrlValue;
}
[/code]

So then I thought maybe I could rewrite it so I pass the set and get accessors. But C# doesnt expose them. If it did, I could use this generic class (which provides some delegates).
<div style="background-color:white; color:black
<pre style="border:solid 1px black <span style="color:blue private <span style="color:blue abstract <span style="color:blue class SaveHelper<type>

{
<span style="color:blue public <span style="color:blue delegate type Get();
<span style="color:blue public <span style="color:blue delegate <span style="color:blue void Set(type value);

<span style="color:blue public <span style="color:blue static <span style="color:blue void CopyValueToFormat(Get <span style="color:blue get, Set <span style="color:blue set, type CtrlValue)
{
<span style="color:blue if(!<span style="color:blue get.Equals(CtrlValue))
<span style="color:blue set(CtrlValue);
}
}
[/code]

Can someone help? I dont want to replace the properties with explicit Set and Get functions. Besides, someone might have gotten into this situation with a 3rd party library.

View the full article
 
Back
Top