开发者

C# & Silverlight Basic Class Syntax with Get/Set Properties not working

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 stri开发者_如何学编程ng.... 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:

private string _displayName = string.Empty;
public string displayName
{
    get { return _displayName; }
    set { _displayName = value; }
}

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:

public class Report : INotifyPropertyChanged
{
    // Declare the PropertyChanged event
    public event PropertyChangedEventHandler PropertyChanged;

    // Raise the PropertyChanged event
    protected void NotifyPropertyChanged(string propertyName)
    {
        var handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }  

At this point, you need to define your properties like:

private string _displayName = string.Empty;
public string DisplayName
{
    get { return _displayName; }
    set 
    { 
        if (_displayName != value)
        {
            _displayName = value; 
            NotifyPropertyChanged("DisplayName");
        }
    }
}

Doing this will allow you to data bind to your "Report" class. You may also want to consider using ObservableCollection<T> instead of List<T> for any collections you want to use with data binding.


You need to be assigning the value of the special variable value in your sets. value is what will be holding the (heh) value that was assigned to your property as it is passed into the set.

 public string reportPath
    {
        get { return _reportPath; }
        set { _reportPath = value; }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜