|
-
January 12th, 2010, 12:09 AM
#1
automatically add getters and setters to priv vars
is there a way that could automate writing getters and setters for private variables in c#?
-
January 12th, 2010, 01:09 AM
#2
Re: automatically add getters and setters to priv vars
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.
-
January 12th, 2010, 03:08 AM
#3
Re: automatically add getters and setters to priv vars
 Originally Posted by glourung
You can use options in visual studio 2008 - just right click and select Wrap local variable.... or something.
Right-click -> Refactor -> Encapsulate field
-
January 12th, 2010, 04:00 AM
#4
Re: automatically add getters and setters to priv vars
If you're using .NET 3.0 or higher you can use the automatic property syntax:
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;
}
Is that what you wanted?
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
January 12th, 2010, 09:34 AM
#5
Re: automatically add getters and setters to priv vars
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:
Code:
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|