> Yeah, I think most people are using some sort of automator,
> and I don't see many variations on what the gen'd code should
> look like, so it seems ripe for formalization - unless there
> would be side effects. Another option would be to have blank
> get/set methods automatically insert that code, kind of like
> 'super' is automated in constructors. I think it would still
> be good to have a 'holder' type keyword available though, as
> you often want that variable for one small extra thing..
I use a macro (for C#) to create properties. First define a
private membervariable, like:
private int _foo;
then type on a line:
int foo
and hit the keycomby I assigned to the macro and I get:
/// <summary>
/// Gets / Sets foo
/// </summary>
public int Foo
{
get
{
return _foo;
}
set
{
_foo = value;
}
}
works great. The member variables are the key here. Properties are just
ways to access them, properties are not 'variables' and should not be
seen as such, they are easy to use 'access' methods to the member
variables, in such a way that you as a developer can control how and
when private member variables are accessed.
The macro:
''' 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
FB
--------------------------------------------------------------------
Get LLBLGen Pro, the new O/R mapper for .NET: http://www.llblgen.com
My .NET Blog : http://weblogs.asp.net/FBouma
--------------------------------------------------------------------
===================================
This list is hosted by DevelopMentor� http://www.develop.com
NEW! ASP.NET courses you may be interested in:
2 Days of ASP.NET, 29 Sept 2003, in Redmond
http://www.develop.com/courses/2daspdotnet
Guerrilla ASP.NET, 13 Oct 2003, in Boston
http://www.develop.com/courses/gaspdotnet
View archives and manage your subscription(s) at http://discuss.develop.com