is there a way that could automate writing getters and setters for private variables in c#?
Printable View
is there a way that could automate writing getters and setters for private variables in c#?
You can use options in visual studio 2008 - just right click and select Wrap local variable.... or something. I pretty sure that you can add hot key for it. If you want full automatic I believe that the easies way is to write some program for it or a plug in for VS which is not very complex.
If you're using .NET 3.0 or higher you can use the automatic property syntax:
Is that what you wanted?Code:// Public getter, private setter
public int MyProperty {
get; private set;
}
// Both are private
int MyOtherProperty {
get; set;
}
// Internal setter, private getter
internal MyLastProperty {
private get; set;
}
You can use code snippets in 2008- I don't know if they are available in previous versions. Just write 'prop' and then immediately hit Tab twice- this will stub out the code for a public property by default, but you can easily change that. It'll pop out something like this:
Just change the type from int to whatever you need, and change the name MyProperty to the name you want to use. You can change it around more by writing private just before set to make it a private set property, etc.Code:public int MyProperty { get; set; }
HTH