471,317 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Tool to generate properties from private fiels

Anyone know a free vs.net 2003 plugin that is capable of generating
properties from private fields for C#? Thanks ahead.

Jan 16 '06 #1
3 1345
Thanks a lot.

Jan 16 '06 #3
Truong Hong Thi wrote:
Anyone know a free vs.net 2003 plugin that is capable of generating
properties from private fields for C#? Thanks ahead.


Use my macro below and bind it to a keycombi, e.g cntrl-shift + n

say you have the field:
private string _foo;

you then type:
string foo

and on that line hit cntrl-shift-n

and you'll get
<summary>Gets / Sets Foo</summary>
public string Foo
{
get { return _foo;}
set { _foo = value;}
}
''' Creates a property.
''' format: type name has to be present at the current line.
''' Uses caMel casing.
Sub CreateProperty()
Dim typeName, propertyName, memberName, nameRead As String
Dim ts As TextSelection
Dim sb As New StringBuilder
Dim boolIsOpened As Boolean

Try
ts = DTE.ActiveDocument.Selection
If (ts.IsEmpty) Then
ts.SelectLine()
End If

typeName = (ts.Text.Split(" ")(0)).Trim()
nameRead = ((ts.Text.Split(" ")(1))).Trim()
propertyName = nameRead.Substring(0, 1).ToUpper &
nameRead.Substring(1)
memberName = "_" + nameRead.Substring(0, 1).ToLower() &
nameRead.Substring(1)

' actual property
sb.Append("/// <summary>" & Environment.NewLine)
sb.AppendFormat("/// Gets / sets {0}{1}", nameRead,
Environment.NewLine)
sb.Append("/// </summary>" & Environment.NewLine)
sb.Append("public " + typeName + " " + propertyName)
sb.Append(vbCrLf)
sb.Append("{")
sb.Append(vbCrLf)

' get
sb.Append("get")
sb.Append(vbCrLf)
sb.Append("{")
sb.Append(vbCrLf)
sb.AppendFormat("return {0};", memberName)
sb.Append(vbCrLf)
sb.Append("}")
sb.Append(vbCrLf)

' set
sb.Append("set")
sb.Append(vbCrLf)
sb.Append("{")
sb.Append(vbCrLf)
sb.AppendFormat("{0} = value;", memberName)
sb.Append(vbCrLf)
sb.Append("}")
sb.Append(vbCrLf)
sb.Append("}")
sb.Append(vbCrLf)
sb.Append(vbCrLf)

'Check to see if UndoContext object is already open.
If DTE.UndoContext.IsOpen = False Then
'Open the UndoContext object to track changes.
Call DTE.UndoContext.Open("CreateProperty " &
propertyName, False)
boolIsOpened = True
End If

' Replace the text
ts.Delete()
ts.Insert(sb.ToString(),
vsInsertFlags.vsInsertFlagsInsertAtStart)
Finally

'If UndoContext was already open, don't close it.
If boolIsOpened = True Then
'Close the UndoContext object to commit the changes.
DTE.UndoContext.Close()
' Format the Selection
ts.SmartFormat()
ts.MoveToPoint(ts.BottomPoint.CreateEditPoint())
ts.LineUp()
End If
End Try
End Sub

Frans
--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Jan 16 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by P Jones | last post: by
7 posts views Thread by George Jordanov Ivanov | last post: by
4 posts views Thread by Anastasios Hatzis | last post: by
3 posts views Thread by Anibal Caixinha | last post: by
2 posts views Thread by vips | last post: by
7 posts views Thread by =?Utf-8?B?TXJOb2JvZHk=?= | last post: by

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.