Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Transform a variable into a Property
[ All Languages » VB.net »  IDE]

Total Hit ( 2597)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Thanks to Visual Studio.NET macros, it is quite simple to automate the task of transforming a Public field into a Property with the same name that accesses a private field of the same type.

Press Alt-F11 (or invoke the Macro IDE from the Tools|Macros submenu, select one macro module in the left-most treeview, and double-click on it to bring up the corresponding code module. Now cut and paste the following procedure:

Click here to copy the following block
Sub MorphVariableIntoProperty()
  DTE.ActiveDocument.Selection.StartOfLine _
    (vsStartOfLineOptions.vsStartOfLineOptionsFirstText)
  DTE.ActiveDocument.Selection.WordRight(True)
  DTE.ActiveDocument.Selection.Delete()
  DTE.ActiveDocument.Selection.Text = "Private m_"
  DTE.ActiveDocument.Selection.EndOfLine(True)
  DTE.ActiveDocument.Selection.Copy()
  DTE.ActiveDocument.Selection.EndOfLine()
  DTE.ActiveDocument.Selection.NewLine(2)
  DTE.ActiveDocument.Selection.Text = "Public Property "
  DTE.ActiveDocument.Selection.Paste()
  DTE.ActiveDocument.Selection.NewLine()
  DTE.ActiveDocument.Selection.LineUp(False, 2)
  DTE.ActiveDocument.Selection.StartOfLine _
    (vsStartOfLineOptions.vsStartOfLineOptionsFirstText)
  DTE.ActiveDocument.Selection.WordRight(False, 2)
  DTE.ActiveDocument.Selection.WordRight(True)
  DTE.ActiveDocument.Selection.Copy()
  DTE.ActiveDocument.Selection.LineDown(False, 2)
  DTE.ActiveDocument.Selection.Indent(2)
  DTE.ActiveDocument.Selection.Text = "Return m_"
  DTE.ActiveDocument.Selection.Paste()
  DTE.ActiveDocument.Selection.LineDown(False, 3)
  DTE.ActiveDocument.Selection.Indent(2)
  DTE.ActiveDocument.Selection.Text = "m_"
  DTE.ActiveDocument.Selection.Paste()
  DTE.ActiveDocument.Selection.Text = "= Value"
End Sub

Next, bring up the Tools-Options dialog box, go to Keyboard page, and assign a shortcut key combination to the Morph§VariableIntoProperty macro. Notice that the list of macros contains complete macro names, that are prefixed by their module name. For example, you can assign the following key combination (two keystrokes): Ctrl-Shift-M, Ctrl-Shift-P. To reduce the odds of clashing with other macros, you can reduce the macro's visibility to the text editor.
Now you're ready. Let's say that you have the following variable declaration:

Click here to copy the following block
Dim Length As Single

Position the caret anywhere on the DIM statement and press the shortcut assigned to the macro defined above. In a few instants Visual Studio renames the variable into m_Length, makes it private, and adds a Public property named Length:

Click here to copy the following block
Private m_Length As Single

  Public Property Length() As Single
    Get
      Return m_Length
    End Get
    Set(ByVal Value As Single)
      m_Length = Value
    End Set
  End Property

Notice that this macro heavily relies on Visual Studio's ability to create a Property procedure template when you press the Enter key on a Property declaration. If this feature is disabled, the macro won't work.


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.