Please feel free to use my macro designed for the purpose.
You should declare property backer fields using an underbar character and a
lower-case first letter like so:
Private int _myInt;
Running the macro on this line creates a property such as:
public int MyInt
{
get{return _myInt;}
set{_myInt=value;}
}
For my own preference I have tied this macro to the Ctrl+Shft+V (for
variable) key combination.
You can define all your fields and then just hit your chosen hot-key
combination repeatedly to turn all fields into public properties with the
correct get-set code.
Full macro code is after my signature.
--
Bob Powell [MVP]
Visual C#, System.Drawing
Ramuseco Limited .NET consulting
http://www.ramuseco.com
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
-------------------------------------------------------
Sub FeildToProperty()
DTE.ActiveDocument.Selection.StartOfLine(vsStartOf LineOptions.vsStartOfLineOptionsFirstColumn)
DTE.ActiveDocument.Selection.EndOfLine(True)
DTE.ActiveDocument.Selection.Copy()
DTE.ActiveDocument.Selection.EndOfLine()
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.StartOfLine(vsStartOf LineOptions.vsStartOfLineOptionsFirstColumn)
DTE.ActiveDocument.Selection.Paste()
DTE.ActiveDocument.Selection.StartOfLine(vsStartOf LineOptions.vsStartOfLineOptionsFirstText)
DTE.ActiveDocument.Selection.WordRight(True)
Dim currtext As String = DTE.ActiveDocument.Selection.Text
If (currtext = "protected " Or currtext = "private " Or currtext = "public
") Then
DTE.ActiveDocument.Selection.Delete()
End If
DTE.ActiveDocument.Selection.EndOfLine()
DTE.ActiveDocument.Selection.StartOfLine(vsStartOf LineOptions.vsStartOfLineOptionsFirstText)
DTE.ActiveDocument.Selection.Text = "public "
DTE.ActiveDocument.Selection.WordRight()
DTE.ActiveDocument.Selection.WordRight(True)
DTE.ActiveDocument.Selection.Copy()
DTE.ActiveDocument.Selection.CharLeft()
DTE.ActiveDocument.Selection.Delete()
DTE.ActiveDocument.Selection.CharRight(True)
DTE.ActiveDocument.Selection.ChangeCase(vsCaseOpti ons.vsCaseOptionsUppercase)
DTE.ActiveDocument.Selection.WordRight()
DTE.ActiveDocument.Selection.EndOfLine(True)
DTE.ActiveDocument.Selection.Delete()
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.Text = "{"
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.Text = "}"
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.LineUp(False, 2)
DTE.ActiveDocument.Selection.EndOfLine()
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.Text = "get{return "
DTE.ActiveDocument.Selection.Paste()
DTE.ActiveDocument.Selection.Text = ";}"
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.Text = "set{"
DTE.ActiveDocument.Selection.Paste()
DTE.ActiveDocument.Selection.Text = "=value;}"
DTE.ActiveDocument.Selection.LineDown(False, 3)
DTE.ActiveDocument.Selection.StartOfLine(vsStartOf LineOptions.vsStartOfLineOptionsFirstText)
End Sub
-------------------------------------------------------
"Thorsten Ottosen" <th***@wmdata.com> wrote in message
news:42***********************@news.sunsite.dk...
Dear all,
I'm new to C#, so forgive my stupid question.
In my question to avoid boilerplate code, I was wondering if
I could use attributes to generate some code for me.
For example
public class Foo
{
[GenerateProperty("Field")]
int m_field;
}
should be equivalent to
public class Foo
{
int m_field;
public int Field
{
get { return m_field; }
set { m_field = value; }
}
}
Is that possible?
Thanks in advance
Thorsten