Καλώς ορίσατε στο dotNETZone.gr - Σύνδεση | Εγγραφή | Βοήθεια
σε

 

Αρχική σελίδα Ιστολόγια Συζητήσεις Εκθέσεις Φωτογραφιών Αρχειοθήκες

Macro για τη μετατροπή variable σε property

Îåêßíçóå áðü ôï ìÝëïò cap. Τελευταία δημοσίευση από το μέλος cap στις 28-01-2005, 12:32. Υπάρχουν 0 απαντήσεις.
Ταξινόμηση Δημοσιεύσεων: Προηγούμενο Επόμενο
  •  28-01-2005, 12:32 1063

    Macro για τη μετατροπή variable σε property

    Αυτό το βρήκα σε μια έτοιμη mdb του CodeLib (http://dotnet.4all.cc) που δίνεται μαζί με την εφαρμογή και είναι πρακτικότατο:

    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:


    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:


    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:


    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.


    Σωτήρης Φιλιππίδης

    DotSee Web Services

    View Sotiris Filippidis's profile on LinkedIn

    DotNetNuke them!
Προβολή Τροφοδοσίας RSS με μορφή XML
Με χρήση του Community Server (Commercial Edition), από την Telligent Systems