473,396 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Can VS.NET automatically create property declarations?

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
Nov 19 '05 #1
2 1348
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
"Charlie@CBFC" <ch*****@comcast.net> wrote in message
news:uk**************@TK2MSFTNGP12.phx.gbl...
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

Nov 19 '05 #2
Thanks! I'll try that out.

Charlie
"S. Justin Gengo" <sjgengo@[no_spam_please]aboutfortunate.com> wrote in
message news:%2****************@TK2MSFTNGP09.phx.gbl...
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
"Charlie@CBFC" <ch*****@comcast.net> wrote in message
news:uk**************@TK2MSFTNGP12.phx.gbl...
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


Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Matt | last post by:
Hi all, We recently upsized two Microsoft Access Databases to SQL. We're using an ADP (2002) as the front end. All the conversion issues have been resolved, except for one: Whenever we...
1
by: Colin Mc Mahon | last post by:
Hi, I am trying to re-write an old url management class I use, and am having problems using the Execute method to create vars. I think i'm missing something simple, as I have used this method...
4
by: Andreas Schmidt | last post by:
Suppose there is a 10,000 lines header file that wildly mixes definitions (the interface) and implementations. Other code that includes this header file takes hours to compile, and it's impossible...
5
by: Nathan Sokalski | last post by:
My teammate on a project somehow removed all the control declarations in a project. Since most of the control declarations are added automatically when the controls are added, is there a way to...
21
by: Simon Verona | last post by:
Hope somebody can help! I want to automatically be able to add code to the initialize routine on a Windows form when I add a custom control that I've written to a form. Specifically, I'm trying...
54
by: MLH | last post by:
I use A97 and do not always insert line numbers while writing procedures. I find it necessary to go back and add them later to aid in debugging. Nearly 3 years ago, something was mentioned in...
1
by: Nathan Sokalski | last post by:
When implementing an interface in VB.NET using Visual Studio .NET 2005, it would be nice if I could have the method/function headers inserted automatically. Whenever we implement an event (such as...
45
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies...
4
by: Vlad | last post by:
I am having problems using the file.create method within a function that is called when looping through an array of filepaths. If I call my function with a hardcoded file path --C:\Temp.txt the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.