Click here to Skip to main content
15,888,816 members
Articles / Web Development / ASP.NET
Tip/Trick

Create property from field by right click

Rate me:
Please Sign up or sign in to vote.
2.50/5 (3 votes)
22 Sep 2013CPOL 9.2K   2   4
Get/set property by right clicking on .NET IDE.

Introduction 

This will help developer to create property by setting the field name and datatype .

Background 

This article will help developer to generate property by right click on field.

Using the code

C#
public class Student  
{ 
   string Username; 
   public string _Username
   {
       get { return Username; }
       set { Username = value; }
   }  
}

Above student class has a field called Username and we have set the property for them. Now in .NET instead of write these code we can generated by right click on field name .

Create a class and field then right click on field.   click on Refactor then Encapsulated Field as show in the image.

Image 1

Following window will popup. 

Image 2

Type the property name in property text box and click on ok

 Following will be generated by .net IDE. 

C#
public string Username1
{ 
  get { return Username; }
  set { Username = value; } 
}

Points of Interest

This option is really helpful for auto generating properties by creating the field type and setting the datatype.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
United States United States
I have 10+ years of experience in IT industry with a wide range of experience in analysis, design, development, implementation of Web Based Applications & Client Server Applications in various Microsoft related Technologies.

Comments and Discussions

 
GeneralMy vote of 1 Pin
dllundin23-Sep-13 4:01
dllundin23-Sep-13 4:01 
QuestionMy vote - 1 Pin
dllundin23-Sep-13 4:00
dllundin23-Sep-13 4:00 
GeneralMy vote of 2 Pin
gicalle7523-Sep-13 1:18
professionalgicalle7523-Sep-13 1:18 
the easiest way to generate a property is by using the build in intellisense autocomplete feature.

usage:
type 'prop' (without quotes) in a class, followed by 2 tabs.

you get a complete get/set function like this:
public int MyProperty { get; set; }
QuestionIsn’t it easier to use the auto-property form? Pin
George Swan22-Sep-13 6:33
mveGeorge Swan22-Sep-13 6:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.