I have a really, really simple class and I am tried to use the get/set properties but they just aren’t working for me… I am sure it is the most obvious thing that I am over looking but I just can’t see why they aren’t working. I have checked out the code that utilizes this class and its fine that I can see.
In the main code, if I type
Report r = new Report();
string str = "Taco";
r.displayName = str;
The report is declared alright and everything is set to empty strings or a new list or whatever the parameter’s default is. But every time I ran this the displayName always remained blank after the code finished executing…
so I tried putting a stop point in the Class displayName set property at set {_displayName = displayName;} and the value always passed in (displayName) was an empty string…. even though the string clearly says “Taco” in the main code…. I have no idea but I am sure its right in my face. If you need more code I can provide it…
Report r = new Report();
string str = "Taco";
r.setReportDisplayName(str);
But for some reason the above works.
public class Report
{
private string _reportPath = string.Empty;
public string reportPath
{
get { return _reportPath; }
set { _reportPath = reportPath; }
}
private string _displayName = string.Empty;
public string displayName
{
get { return _displayName; }
set { _displayName = displayName; }
}
private List<parameter> _parameters = new List<parameter>();
public List<parameter> parameters
{
get { return _parameters; }
set { _parameters = parameters; }
}
public Report() { }
public Report(string path, string display, List<parameter> param)
{
_reportPath = path;
_displayName = display
_parameters = param;
}
public void setReportDisplayName(string str)
{
_displayName = str;
}
}
You are defining your properties incorrectly. This should be done as:
That being said, if you are using this for Silverlight, you most likely will want to implement
INotifyPropertyChanged. Without this, data binding will not reflect changes made in code.To implement this interface, you’ll need to add this implementation. A “standard” way to implement this is via:
At this point, you need to define your properties like:
Doing this will allow you to data bind to your “Report” class. You may also want to consider using
ObservableCollection<T>instead ofList<T>for any collections you want to use with data binding.