Wednesday, September 21, 2005

Fields to Properties (Visual Studio.Net)

Yes, we miss Automatic property generation from the Fields of the classes in the Visual Studio.net IDE.
Following is the sample code of the Macro, I am telling here which I used for myself.
This member variable to Property generation is not generic and is very difficult to be. The idea is that I demonstrate to do it my way and you can customize it for your needs.

Code is in the Visual Basic.Net
--------------------------------------------------
NEED:

Public Class AClass

#Region "Members"
Private strStaffId As String
Private strStaffName As String
#End Region

End Class

-->
Public Class AClass

#Region "Members"
Private strStaffId As String
Public Property StaffId() As String
Get
Return strStaffId
End Get
Set(ByVal Value As String)
strStaffId = Value
End Set
End Property

Private strStaffName As String
Public Property StaffId() As String
Get
Return strStaffId
End Get
Set(ByVal Value As String)
strStaffId = Value
End Set
End Property

#End Region

End Class

--------------------------------------------------

The first step in using a macro to generate code is to open the Macros IDE, add a new module, a macro, and stub out the code template.

1. To create a new macro, open Visual Studio .NET—I am using VS.NET 2003, but the example works in version 1—and select Tools|Macros|Macros IDE
2. In the Macros Project Explorer click on the MyMacros project, right-clicking Add|Add Module from the Project Explorer context menu
3. Add a public subroutine named WriteProperty to the module

So, In my case it is like:
File:- MyModule
Imports EnvDTE
Imports System
Imports System.Diagnostics

Public Module MyModule

Private Cr As String = Environment.NewLine

Private mask As String = _
"Public Property {0}() As {1}" Cr _
" Get" Cr _
" Return {2}" Cr _
" End Get" Cr _
" Set(ByVal Value As {1})" Cr _
" {2} = Value" Cr _
" End Set" Cr _
" End Property" Cr

Public Sub WriteProperty()
Dim Selection As TextSelection = DTE.ActiveDocument.Selection

Dim FieldName As String
Dim PropertyName As String
Dim PropertyType As String

FieldName = Selection.Text
PropertyName = FieldName.Substring(3) 'skip strUserName -> UserName

Selection.EndOfLine()
Selection.WordLeft(True)
PropertyType = Selection.Text
Selection.EndOfLine()
Selection.NewLine(2)

Dim vp As VirtualPoint = Selection.ActivePoint
Selection.Insert(String.Format(mask, PropertyName, PropertyType, FieldName))
End Sub
End Module
-----------------------------------------------------------------------

Now, You can customize the VisualStudio environment and put a button on a toolbar and that button can call our macro. You can also assign a hot key to call this code.
To generate the property for the field, You only have to select the field and it will generate its code for the corresponding property.

When done, you only need to edit this code according to your needs.

bye
:)

3 comments:

Qazi said...

But why you are blogging at two places? insead of writing at your space only.

hB said...

Perhaps now code-programming blogs at blogspot and rest at msn spaces.
I still could not figure how to manage this at best..

hB said...

http://spaces.msn.com/members/habibcs