473,387 Members | 1,573 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

automatic code generation for properties

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
5 7429
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Rasmus Fogh | last post by:
Someone raised the question of automatic code generation a few weeks back. And yes, we (CCPN) are using automatic Python code generation in a major way. Basically we are making data models in...
15
by: Kannan Goundan | last post by:
Maintaining a C++ header file can be painful. I want a way to automatically generate header files from the implementation file. Does anybody know of a program that does this? If not, I'd like...
29
by: Natan | last post by:
When you create and aspx page, this is generated by default: using System; using System.Collections; using System.Collections.Specialized; using System.Configuration; using System.Text; using...
4
by: Petterson Mikael | last post by:
Hi, Anyone out there that knows of a automatic test generation tool for cpp? Another requirement is that the test results should be presented in xml. All hints appreciated. cheers, ...
0
by: JoshforRefugee | last post by:
heard that we can do automatic code generation using macros, but not sure how can I pursue this. Here is my problem. In my env, I have class A,B and C. All of them has constructors, and few...
3
by: Chris288 | last post by:
Hi, I have a problem where our app when compiled in VS2005 runs about 50% the speed it attains in VS2003. This is an unmanaged C++ app. I have tried most combinations of the optimization and...
17
by: Sunburned Surveyor | last post by:
I was thinking of a way I could make writing Python Class Files a little less painful. I was considering a Ptyhon script that read a file with a list of property names and method names and then...
25
by: sidd | last post by:
In the following code: int i = 5; ---it goes to .data segment int j; ---it goes to bss segment int main() { int c; int i = 5; ---stack
34
by: =?ISO-8859-1?Q?Marcel_M=FCller?= | last post by:
Hi, is there a way to avoid the automatic copy constructor generation. I do not want the object to be non-copyable. I simply do not want that copying is done by the default copy constructor. But...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.