Get and set

  • Thread starter Thread starter Arjen
  • Start date Start date
A

Arjen

Hi,

Is there a utility in C# or in visual web developer to automaticly create
get and set code lines for a given variable?

Thanks!
 
Yes. In Visual Studio 2005, you can right-click on a field definition
and select Refactor > Encapsulate Field. This will convert the field to
a property, writing set and get accessors, and let you give the
property a different name while you're at it (updating the rest of your
code to refer to the new name).

Jesse
 
Hi Arjen,

I wrote a small program to generate my basic class structure with gets
and sets etc. Let me know if you want the code and/or application.

Happy Coding,

Stefan
C# GURU
www.DotNETovation.com

"You always have to look beyond the horizon and can never be complacent
-- God forbid we become complacent."

Jozef Straus
 
You can use my macro...

It expects all feilds to start with an underscore and a lower-case letter
such as:

int _myInt;

It converts to a property removint the underscore and capitalizing the first
letter.

This is done because when I use a tool to convert from C# to VB.NET I don't
get problems with VB complaining in it's lame way about myInt and MyInt
being the same. (idiotic language!!!)
Sub FeildToProperty()

DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn)

DTE.ActiveDocument.Selection.EndOfLine(True)

DTE.ActiveDocument.Selection.Copy()

DTE.ActiveDocument.Selection.EndOfLine()

DTE.ActiveDocument.Selection.NewLine()

DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn)

DTE.ActiveDocument.Selection.Paste()

DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.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(vsStartOfLineOptions.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(vsCaseOptions.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(vsStartOfLineOptions.vsStartOfLineOptionsFirstText)

End Sub


--
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.
 
Back
Top