Click to See Complete Forum and Search --> : automatically add getters and setters to priv vars


niladhar8@gmail.com
January 11th, 2010, 11:09 PM
is there a way that could automate writing getters and setters for private variables in c#?

glourung
January 12th, 2010, 12:09 AM
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.

dannystommen
January 12th, 2010, 02:08 AM
You can use options in visual studio 2008 - just right click and select Wrap local variable.... or something.

Right-click -> Refactor -> Encapsulate field

Mutant_Fruit
January 12th, 2010, 03:00 AM
If you're using .NET 3.0 or higher you can use the automatic property syntax:


// 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;
}


Is that what you wanted?

CreganTur
January 12th, 2010, 08:34 AM
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:
public int MyProperty { get; set; }
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.

HTH