C9_CloudX

Quick question on automatic properties, is it possible to add a call to a safe notification method in the "set". This would allow the use of autoprops and INotifyPropertyChanged. I can't see one for the life of me.

Kind Regards,
Luke Clodusdale



Re: Visual C# 2008 (Pre-release) Automatic Properties and INotifyPropertyChanged

Peter Ritchie

You'll have to implement the properties to do anything other than automatic use of a backing store.

Are you asking as a suggestion






Re: Visual C# 2008 (Pre-release) Automatic Properties and INotifyPropertyChanged

C9_CloudX

I would suggest that in the next iteration the ability to add a method call in the set block be considered. In the case of INotifyPropertyChanged this would help a lot.





Re: Visual C# 2008 (Pre-release) Automatic Properties and INotifyPropertyChanged

Peter Ritchie

It's very little work to support INotifyPropertyChanged with non-automatic properties.

Code Snippet

private int value

public int Value {

get { return this.value;}

set { this.value = value; OnNotifyPropertyChanged("Value");}

}

from

Code Snippet
public int Value {get; set;}

The syntax of automatic properties can work because in C# 2.0 and less that syntax is erroneous, this is no block in set or get. By adding code blocks (e.g. "set block") to automatic properties you no longer have a way to distinguish automatic properties from non-automatic properties and you'd need to add some sort of new syntax or keyword--which is likely something that was avoided in this iteration of automatic properties and would then require two syntaxes for automatic properties.






Re: Visual C# 2008 (Pre-release) Automatic Properties and INotifyPropertyChanged

C9_CloudX

Yep fair enough, thats basically how I implement them, it would be good then to have an inbuilt code snippet to either support non automatic properties or better still a property with notification. You can create one yourself which is good but one inbuilt would help a lot of people straight from the outset. Its not a big thing but always helps.

Cheers,

Luke





Re: Visual C# 2008 (Pre-release) Automatic Properties and INotifyPropertyChanged

Peter Ritchie

There's a couple of good property snippets ("prop" and "propg", where "propg" is a read-only property). You can modify what is created when you use those snippets, or copy and modify.

There's also some handy refactoring tools. I.e., if you have this:

Code Snippet

public int value;

right-clicking on "value", you can select Refactor\Encapsulate Field to turn it into this:

Code Snippet

private int value;

public int Value
{
get { return this.value; }
set { this.value = value; }
}

I believe you can also modify what is generated by refactoring too.

You can also drag text selections onto the Toolbox to later be drag-copied to other files (in other projects, if necessary).






Re: Visual C# 2008 (Pre-release) Automatic Properties and INotifyPropertyChanged

C9_CloudX

Yep what I did is create a new code snippet propn that did what I needed.

Thanks for the suggestions, they were helpful!





Re: Visual C# 2008 (Pre-release) Automatic Properties and INotifyPropertyChanged

Hacker42

Peter Ritchie wrote:

It's very little work to support INotifyPropertyChanged with non-automatic properties.

Code Snippet

private int value

public int Value {

get { return this.value;}

set { this.value = value; OnNotifyPropertyChanged("Value");}

}



This approach is problematic due to several reasons:

1. The property name is duplicated, and even worse, it is written inside a STRING! Very dangerous and error prone.

2. Tedious and repetitive. Code snippets\refactoring can help create these lines easily, but the end result is still code duplication.

3. Can't use automatic properties anymore...



BUT FEAR NOT! You can get exactly what you want by using PostSharp, an open source Aspect Oriented Programming tool for .NET


Then you just add an attribute to the class and property notification happens "magically". I use it for a similar reason in my projects, works beautifully even with automatic properties! Look at the PostSharp site for examples.



Have fun :-)





Re: Visual C# 2008 (Pre-release) Automatic Properties and INotifyPropertyChanged

Peter Ritchie

Hacker42 wrote:
Peter Ritchie wrote:

It's very little work to support INotifyPropertyChanged with non-automatic properties.

Code Snippet

private int value

public int Value {

get { return this.value;}

set { this.value = value; OnNotifyPropertyChanged("Value");}

}



This approach is problematic due to several reasons:

1. The property name is duplicated, and even worse, it is written inside a STRING! Very dangerous and error prone.

2. Tedious and repetitive. Code snippets\refactoring can help create these lines easily, but the end result is still code duplication.

3. Can't use automatic properties anymore...



BUT FEAR NOT! You can get exactly what you want by using PostSharp, an open source Aspect Oriented Programming tool for .NET


Then you just add an attribute to the class and property notification happens "magically". I use it for a similar reason in my projects, works beautifully even with automatic properties! Look at the PostSharp site for examples.



Have fun :-)

Point 1 is just a comment about INotifyPropertyChanged. Use of a hard-coded string for the parameter to OnNotifyPropertyChanged is error prone, yes; but not "dangerous". If you're really concerned about using a hard-code string there, you could reflect on the current method and get its name. For example:

Code Block

int Number

{

set

{

this.number = value;

MethodBase mb = MethodBase.GetCurrentMethod();

String propertyName = mb.Name.Substring(4);

OnNotifyPropertyChanged(propertyName);

}

}