By "generate", I mean auto-generation of the code necessary for a particuliar selected (set of) variable(s).
But any more explicit explication or comment on good practice is welcome.
By "generate", I mean auto-generation of the code necessary for a particuliar selected (set of) variable(s).
But any more explicit explication or comment on good practice is welcome.
I don't have Visual Studio installed on my machine anymore (and I'm using Linux), but I do remember that there was an wizard hidden somewhere inside one of the menus that gave access to a class builder.
With this wizard, you could define all your classes' details, including methods and attributes. If I remember well, there was an option through which you could ask VS to create the setters and getters automatically for you.
I know it's quite vague, but check it out and you might find it.
By generate, do you mean auto-generate? If that's not what you mean:
Visual Studio 2008 has the easiest implementation for this:
public PropertyType PropertyName { get; set; }
In the background this creates an implied instance variable to which your property is stored and retrieved.
However if you want to put in more logic in your Properties, you will have to have an instance variable for it:
private PropertyType _property;
public PropertyType PropertyName
{
get
{
//logic here
return _property;
}
set
{
//logic here
_property = value;
}
}
Previous versions of Visual Studio always used this longhand method as well.
If you are using Visual Studio 2005 and up you can create a setter/getter real fast using the insert snippet command. Right click on your code click on Insert Snippet (Ctrl+k,x) and then choose "prop" form the list. Hope this helps.
Rather than using ctrl+k,x you can also just type prop
and then hit tab twice
Visual Studio also has a feature that will generate a Property from a private variable.
If you right-click on a variable, in the context menu that pops up click on the "Refactor" item. Then choose encapsulate field. This will create a getter/setter property for a variable.
I'm not too big a fan of this technique as it is a little bit awkward to use if you have to create a lot of getters/setters, and it puts the property directly below the private field, which bugs me because I usually have all of my private fields grouped together, and this Visual Studio feature breaks my class' formatting.
In addition to the 'prop' snippet and auto-properties, there is a refactor option to let you select an existing field and expose it via a property. Also, if you don't like the 'prop' implementation, you can create your own snippets. Additionally, a 3rd party refactoring tool like resharper will give you even more features and make it easier to create more advanced snippets. I'd recommend Resharper if you can afford it.
http://msdn.microsoft.com/en-us/library/f7d3wz0k(VS.80).aspx http://www.jetbrains.com/
If you're using ReSharper, go into the ReSharper menu --> Code --> Generate ... (or hit Alt+Ins inside the surrounding class) and you'll get all the options for generating getters and/or setters you can think of :-)