By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,448 Members | 1,412 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,448 IT Pros & Developers. It's quick & easy.

automatic code generation for properties

P: n/a
Hello All,

There is a particular feature which I want to have in visual studio .net

suppose I create a class

ClassA
{
private string name;
private int age;
}

Then I right click on my code and say "generate properties".

And a property gets created (with capital for the first character ex.
Name, Age) for each of the private members of my class.

Can I do this is vs.net?

thanks for your help in advance.

regards,
Abhishek.
Nov 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
You can't do this right now. Whidbey will include refactoring, which might
have something similar, although it's more likely that you'll be able to
turn simple public members (as your example below) into public get/set
accessors paired with private members.

You can of course write an add-in that will do this.
--
____________________
Klaus H. Probst, MVP
http://www.vbbox.com/

"Abhishek Srivastava" <ab*****************@nospam.net> wrote in message
news:uM**************@TK2MSFTNGP10.phx.gbl...
Hello All,

There is a particular feature which I want to have in visual studio .net

suppose I create a class

ClassA
{
private string name;
private int age;
}

Then I right click on my code and say "generate properties".

And a property gets created (with capital for the first character ex.
Name, Age) for each of the private members of my class.

Can I do this is vs.net?

thanks for your help in advance.

regards,
Abhishek.

Nov 15 '05 #2

P: n/a
Abhishek Srivastava wrote:
Hello All,

There is a particular feature which I want to have in visual studio .net

suppose I create a class

ClassA
{
private string name;
private int age;
}

Then I right click on my code and say "generate properties".

And a property gets created (with capital for the first character ex.
Name, Age) for each of the private members of my class.

Can I do this is vs.net?

thanks for your help in advance.

regards,
Abhishek.


Check out this artical on code generation:
http://www.devcity.net/net/article.aspx?alias=codedom_1
Nov 15 '05 #3

P: n/a
In the next version, VS will have some built-in refactoring tools that do
similar things (more than just properties though).
Until then, do some searching on the net for VS add-ins. People have written
some that auto-generate properties and the like.

If you're willing to do some work yourself, you can actually create VS
macros that do exactly what you want. Just go to Tools->Macros, and you can
create a script that reads the current selection in the document window, and
uses the code DOM (document object model) to insert property statements, or
do whatever else you like. Macros are EXTREMELY underused IMO :-)

-Rob Teixeira [MVP]

"Abhishek Srivastava" <ab*****************@nospam.net> wrote in message
news:uM**************@TK2MSFTNGP10.phx.gbl...
Hello All,

There is a particular feature which I want to have in visual studio .net

suppose I create a class

ClassA
{
private string name;
private int age;
}

Then I right click on my code and say "generate properties".

And a property gets created (with capital for the first character ex.
Name, Age) for each of the private members of my class.

Can I do this is vs.net?

thanks for your help in advance.

regards,
Abhishek.

Nov 15 '05 #4

P: n/a
Just dont hold your breath for it to refactor winforms with .resx files.
"Rob Teixeira [MVP]" <RobTeixeira@@msn.com> wrote in message
news:OE****************@TK2MSFTNGP10.phx.gbl...
In the next version, VS will have some built-in refactoring tools that do
similar things (more than just properties though).
Until then, do some searching on the net for VS add-ins. People have written some that auto-generate properties and the like.

If you're willing to do some work yourself, you can actually create VS
macros that do exactly what you want. Just go to Tools->Macros, and you can create a script that reads the current selection in the document window, and uses the code DOM (document object model) to insert property statements, or do whatever else you like. Macros are EXTREMELY underused IMO :-)

-Rob Teixeira [MVP]

"Abhishek Srivastava" <ab*****************@nospam.net> wrote in message
news:uM**************@TK2MSFTNGP10.phx.gbl...
Hello All,

There is a particular feature which I want to have in visual studio .net

suppose I create a class

ClassA
{
private string name;
private int age;
}

Then I right click on my code and say "generate properties".

And a property gets created (with capital for the first character ex.
Name, Age) for each of the private members of my class.

Can I do this is vs.net?

thanks for your help in advance.

regards,
Abhishek.


Nov 15 '05 #5

P: n/a
Abhishek Srivastava <ab*****************@nospam.net> wrote:
There is a particular feature which I want to have in visual studio .net

suppose I create a class

ClassA
{
private string name;
private int age;
}

Then I right click on my code and say "generate properties".

And a property gets created (with capital for the first character ex.
Name, Age) for each of the private members of my class.

Can I do this is vs.net?


Others have answered the question you've actually asked, but I'd like
to propose a different solution: namely a way of the C# language itself
changing to allow "simple" property definitions. The syntax is up for
grabs, but I'd suggest something like:

<AccessModifier> property <TypeName> <fieldName> [ = <initialValue>]
<PropertyName> {[get;][set;]}

For instance:

public property string name Name { get; }
protected property int height=10 Height {get; set; }

would be exactly equivalent to:

private string name;
public string Name
{
get { return name; }
}

private int height=10;
protected int Height
{
get { return height; }
set { height = value; }
}

XML documentation applied to the short form would either be applied to
the field, the property or both - up for grabs, but I'd favour the
property, with an automatic XML documentation for the field of "Field
backing the simple property <PropertyName>: <XML doc for PropertyName>"

I was seriously anti this a while ago, and argued fairly vehemently
against it - but I now believe it would make a lot of code
significantly simpler, and might persuade people to use properties
where they might otherwise use public fields just for convenience.

Whether or not one could declare the property to be virtual, I don't
know - and likewise exactly how different access modifiers for get/set
would work.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.