Click here to Skip to main content
5,785,816 members and growing! (18,120 online)
Email Password   helpLost your password?
Development Lifecycle » Code Generation » General     Beginner License: The Code Project Open License (CPOL)

Generate Properties from Class Variables in Visual Studio .NET

By Adeel Hussain

Generate properties from class variables using macro
VB, .NET, Dev

Posted: 25 Nov 2008
Updated: 25 Nov 2008
Views: 2,198
Bookmarked: 14 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
3 votes for this Article.
Popularity: 1.43 Rating: 3.00 out of 5
1 vote, 33.3%
1
0 votes, 0.0%
2
1 vote, 33.3%
3
0 votes, 0.0%
4
1 vote, 33.3%
5

Introduction

One of the nice things about Visual Studio is its macro support. You can create and use macros in Visual Studio .NET to increase productivity. One feature which most developers want is a quick property generation from a list of class variables, but Visual Studio .NET has no shortcut for it. To achieve this, you have to record or create a macro.

So below is the macro that generates the automatic properties from the member variables.

Just copy the text below and then open Visual Studio .NET. Go to Tools-> Macro->Macros IDE menu item. A new window will open. Click on mymacros and paste the below text and build it.

After building, open your Visual Studio project. Go to Tools->Options. A dialog will appear. Click on keyboard in Environment. Select your macro and assign a shortcut. For example Shift + F2.

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics
Imports System.Text

Public Module Expand
Public Sub ExpandPrivateMembersFronSelection()
Dim TS As TextSelection = 

DTE.ActiveDocument.Selection
Dim strResult As String, Line As String
Dim Lines() As String = 

TS.Text.Split(vbNewLine)
For Each Line In Lines
strResult &= GetstrResult(Line)
Next
TS.EndOfLine()
TS.NewLine()
TS.Insert(vbCrLf & "#region ""Public 

Properties""" & vbCrLf & strResult & 

"#endregion" & vbCrLf)

DTE.ExecuteCommand("Edit.FormatDocument")
End Sub

Private Function GetstrResult(ByVal text As 

String) As String
Try
If (text.Contains("private")) Then
text = text.Replace("private", 

"")
End If
If (text.Contains(";")) Then
text = text.Replace(";", "")
End If
Dim words() As String = 

text.Trim.Split()

'System.Windows.Forms.MessageBox.Show(words(1))
If words.Length < 2 Then
Return ""
Else
Dim strResult As New 

StringBuilder
strResult.AppendLine()

strResult.AppendLine(String.Format("public {0} 

{1}", words(0), StrConv(words(1)(0), 

VbStrConv.ProperCase)) & words(1).Substring(1))
strResult.AppendLine(vbTab & "{ 

get { ")
strResult.AppendLine(vbTab & 

vbTab & "return " & words(1) & ";")
strResult.AppendLine(vbTab & 

"}")

strResult.AppendLine(String.Format(vbTab & 

"set"))
strResult.AppendLine(vbTab & 

vbTab & " { " & words(1) & " = value;")
strResult.AppendLine(vbTab & 

"}")
strResult.AppendLine("}")
Return strResult.ToString
End If
Catch ex As Exception

System.Windows.Forms.MessageBox.Show(ex.ToString

)
End Try

End Function

End Module

Issues

Currently it will give you incorrect results if the input is:

string p = string.Empty;

You can customize the macro according to your needs. The valid inputs are:

string p;

private string q;

Just select and press your assigned shortcut.

History

  • 25th November, 2008: Initial post

License

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

About the Author

Adeel Hussain



Occupation: Web Developer
Company: Allainet (Pvt) Ltd
Location: Pakistan Pakistan

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 1 of 1 (Total in Forum: 1) (Refresh)FirstPrevNext
GeneralNice EffortmemberMuhammad Shahid Farooq0:25 26 Nov '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 25 Nov 2008
Editor: Deeksha Shenoy
Copyright 2008 by Adeel Hussain
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project