Property Generator?

S

slonocode

Is there any utility that will generate generic property statements for
a selection of private fields?

private _mystring as string

WOULD GET CONVERTED TO

public readonly property mystring(byval value as string) as string
get
return _mystring
set
_mystring = value
property


It gets pretty tedious typing out property declarations especially if
you have a number of fields.

Thanks
Slonocode
 
J

Jan Hyde

slonocode <[email protected]>'s wild thoughts were
released on Mon, 24 Jan 2005 20:22:17 -0500 bearing the
following fruit:
Is there any utility that will generate generic property statements for
a selection of private fields?

private _mystring as string

WOULD GET CONVERTED TO

public readonly property mystring(byval value as string) as string
get
return _mystring
set
_mystring = value
property


It gets pretty tedious typing out property declarations especially if
you have a number of fields.

MZTools (www.mztools.com) does this and a whole lot more.





Jan Hyde (VB MVP)
 
C

Chris Dunaway

Here's a macro you can use from VS. I didn't write this but it seems
to work fairly well and you can customize to your liking. You can add
a button to the toolbar and have it execute this macro. To use the
macro, highlight the private declaration of the variable and then run
the macro. It will create the property definitions. Watch for line
breaks.

'BEGIN CODE HERE

Imports EnvDTE
Imports System.Diagnostics

Public Module Module1
Public Sub PropertFromVar()
Dim ts As TextSelection = DTE.ActiveWindow.Selection
Dim m_PropertyList(ts.TextRanges.Count - 2) As String
Dim m_Index As Integer
Dim start As EditPoint = ts.TopPoint.CreateEditPoint()
Dim endpt As TextPoint = ts.BottomPoint

Try
Do While (start.LessThan(endpt))
m_PropertyList(m_Index) =
start.GetText(start.LineLength).Trim.Replace("New", "").Replace("()",
"")
start.LineDown()
start.StartOfLine()
m_Index += 1
Loop
Catch ex As System.Exception
Debug.WriteLine(ex)
Finally
'If an error occured, then need to make sure that the undo
context is cleaned up.
'Otherwise, the editor can be left in a perpetual undo
context
End Try

Dim cnt As Integer = m_Index

For m_Index = 0 To m_PropertyList.GetUpperBound(0)
InsertProperty(m_PropertyList(m_Index))
Next

DTE.ExecuteCommand("Edit.FormatDocument")

End Sub

Private Sub InsertProperty(ByVal prop As String)
Dim pmember As String = prop.Substring(prop.IndexOf(" "c)).Trim
Dim ptype As String = pmember.Substring(pmember.IndexOf("As"))
Dim pname As String = pmember.Substring(2)

pname = pname.Substring(0, pname.IndexOf("As")).Trim
pmember = pmember.Substring(0, pmember.Length -
ptype.Length).Trim

Dim ts As TextSelection = DTE.ActiveWindow.Selection


ts.MoveToPoint(ts.ActivePoint.CodeElement(vsCMElement.vsCMElementClass).GetEndPoint(vsCMPart.vsCMPartWhole))
ts.LineUp()
ts.EndOfLine()
ts.NewLine()
ts.NewLine()

ts.Insert("Public Property " & pname & "() " & ptype)
ts.NewLine()
ts.Insert("Return " & pmember)
ts.LineDown(count:=2)
ts.StartOfLine()
ts.Delete(ts.ActivePoint.LineLength)
ts.Insert(Chr(9))
ts.Insert("Set (byval Value " & ptype & ")")
ts.NewLine()
ts.Insert(pmember & "= Value")
ts.LineDown()
ts.Delete()
End Sub

End Module

'CODE ENDS HERE
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top