Can VS.NET automatically create property declarations?

C

Charlie@CBFC

Hi:

I'm getting tired of typing get/set property definitions especially when my
class has a lot of fields. Is there a way to have the VS.NET IDE declare
them automatically?

Thanks,
Charlie
 
S

S. Justin Gengo

Charlie,

You could create a macro.

Here's one I created that I think will do just what you want and has been
very helpful to me...

Public Sub CreatePublicProperties()

Dim TS As TextSelection = GetTextSelection()

Dim Insertion As New System.Text.StringBuilder

Dim Line As String

Dim Lines() As String = TS.Text.Split(vbNewLine)

Dim ReturnedText As String

Dim LineCount, LineLoop As System.Int32

LineCount = Lines.GetUpperBound(0)

For LineLoop = 0 To LineCount

ReturnedText = GetPropertyInsertion(Lines.GetValue(LineLoop))

Insertion.Append(ReturnedText)

Insertion.Append(InsertLine(LineLoop, LineCount, ReturnedText))

Next

TS.EndOfLine()

TS.LineDown(False, 1)

TS.EndOfLine()

TS.NewLine(2)

TS.StartOfLine()

TS.Text = "#Region ""Public Properties"""

TS.NewLine()

TS.Insert(Insertion.ToString)

TS.LineDown(False, 2)

End Sub

Private Function GetPropertyInsertion(ByVal text As String) As String

Dim Words() As String = text.Trim.Split()

' Check if the line is a comment line.

If Words(0).IndexOf("'") = 0 Then

'---This is a comment line include it.

Return text

Else

If Words.Length > 3 Then

'---This is a variable declaration

Dim Insertion As New System.Text.StringBuilder

Insertion.Append(vbTab & "Public Property " & Words(1).Trim("_"))

Insertion.Append(" As " & Words(3))

Insertion.Append(vbNewLine)

Insertion.Append(vbTab & vbTab & "Get")

Insertion.Append(vbNewLine)

Insertion.Append(vbTab & vbTab & vbTab & "Return " & Words(1))

Insertion.Append(vbNewLine)

Insertion.Append(vbTab & vbTab & "End Get")

Insertion.Append(vbNewLine)

Insertion.Append(vbTab & vbTab & "Set(ByVal Value As " & Words(3) & ")")

Insertion.Append(vbNewLine)

Insertion.Append(vbTab & vbTab & vbTab & Words(1) & " = Value")

Insertion.Append(vbNewLine)

Insertion.Append(vbTab & vbTab & "End Set")

Insertion.Append(vbNewLine)

Insertion.Append(vbTab & "End Property")

Return Insertion.ToString

End If

End If

Return ""

End Function





Highlight a list of variable/object declarations like:

Private _MyInt As Int32

Private _MyString As String



And then call the macro and it outputs:



#Region "Public Properties"

Public Property MyInt As Int32

Get

Return _MyInt

End Get

Set(ByVal Value As Int32)

_MyInt = Value

End Set

End Property

Public Property MyString As String

Get

Return _MyString

End Get

Set(ByVal Value As String)

_MyString = Value

End Set

End Property


--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
C

Charlie@CBFC

Thanks! I'll try that out.

Charlie
S. Justin Gengo said:
Charlie,

You could create a macro.

Here's one I created that I think will do just what you want and has been
very helpful to me...

Public Sub CreatePublicProperties()

Dim TS As TextSelection = GetTextSelection()

Dim Insertion As New System.Text.StringBuilder

Dim Line As String

Dim Lines() As String = TS.Text.Split(vbNewLine)

Dim ReturnedText As String

Dim LineCount, LineLoop As System.Int32

LineCount = Lines.GetUpperBound(0)

For LineLoop = 0 To LineCount

ReturnedText = GetPropertyInsertion(Lines.GetValue(LineLoop))

Insertion.Append(ReturnedText)

Insertion.Append(InsertLine(LineLoop, LineCount, ReturnedText))

Next

TS.EndOfLine()

TS.LineDown(False, 1)

TS.EndOfLine()

TS.NewLine(2)

TS.StartOfLine()

TS.Text = "#Region ""Public Properties"""

TS.NewLine()

TS.Insert(Insertion.ToString)

TS.LineDown(False, 2)

End Sub

Private Function GetPropertyInsertion(ByVal text As String) As String

Dim Words() As String = text.Trim.Split()

' Check if the line is a comment line.

If Words(0).IndexOf("'") = 0 Then

'---This is a comment line include it.

Return text

Else

If Words.Length > 3 Then

'---This is a variable declaration

Dim Insertion As New System.Text.StringBuilder

Insertion.Append(vbTab & "Public Property " & Words(1).Trim("_"))

Insertion.Append(" As " & Words(3))

Insertion.Append(vbNewLine)

Insertion.Append(vbTab & vbTab & "Get")

Insertion.Append(vbNewLine)

Insertion.Append(vbTab & vbTab & vbTab & "Return " & Words(1))

Insertion.Append(vbNewLine)

Insertion.Append(vbTab & vbTab & "End Get")

Insertion.Append(vbNewLine)

Insertion.Append(vbTab & vbTab & "Set(ByVal Value As " & Words(3) & ")")

Insertion.Append(vbNewLine)

Insertion.Append(vbTab & vbTab & vbTab & Words(1) & " = Value")

Insertion.Append(vbNewLine)

Insertion.Append(vbTab & vbTab & "End Set")

Insertion.Append(vbNewLine)

Insertion.Append(vbTab & "End Property")

Return Insertion.ToString

End If

End If

Return ""

End Function





Highlight a list of variable/object declarations like:

Private _MyInt As Int32

Private _MyString As String



And then call the macro and it outputs:



#Region "Public Properties"

Public Property MyInt As Int32

Get

Return _MyInt

End Get

Set(ByVal Value As Int32)

_MyInt = Value

End Set

End Property

Public Property MyString As String

Get

Return _MyString

End Get

Set(ByVal Value As String)

_MyString = Value

End Set

End Property


--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 

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

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top