C#: Barenaked Properties?
Keith asks why, why, why would someone write a class full of properties that access a private member but have no custom behavior.
I agree with him up to a point, and that point is "WinForms Data Binding".
Properties and member fields are treated differently by System.Reflection. You can databind to an object property, but not a plan old member. Assuming a form with a button and a text field, the code below will throw an exception when you click the button:
You can't data bind to a public member. Though if you wanted to get really gross you could fake it by implementing your own Type Descriptor that synthesized properties, but we're not going to go there.
I agree with him up to a point, and that point is "WinForms Data Binding".
Properties and member fields are treated differently by System.Reflection. You can databind to an object property, but not a plan old member. Assuming a form with a button and a text field, the code below will throw an exception when you click the button:
using System;
using System.Windows.Forms;
namespace BindingTest
{
public partial class Form1 : Form
{
DataStuff stuff;
public Form1()
{
InitializeComponent();
stuff = new DataStuff();
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.DataBindings.Add("A", stuff, "Text");
}
class DataStuff
{
public string A;
public string B;
public DataStuff()
{
this.A = "Abcde";
this.B = "Defgh";
}
}
}
}You can't data bind to a public member. Though if you wanted to get really gross you could fake it by implementing your own Type Descriptor that synthesized properties, but we're not going to go there.