kamagra how much to take

Auto-Implemented Properties and Developing WebParts

Auto-Implemented (or just automatic) properties are one of the new features in C# 3.0 that allow property declaration to be additionally succinct, lessening compulsory code thereby lightening maintenance and increasing readability. Auto-properties make SharePoint code, which tends to be property heavy for personalization features much easier to write, (sure, you can use the “prop” code snippet in VS.NET, however it still will subscribe to the old property structure) read, and maintain.

Firstly, let’s take a basic SharePoint class, OrthodoxWebPartClass in the below, which uses a conventional property declaration with minimal relevant attributes. The class doesn’t do much outside of rendering the text property to the output stream.

  1. public class OrthodoxWebPartClass : WebPart
  2. {
  3. private string _text;
  5. [Personalizable(PersonalizationScope.Shared),  WebBrowsable(true),  WebDisplayName("Text ToDisplay"), WebDescription("The Text To Display")]
  6. public string Text
  7. {
  8. get { return _text; }
  9. set { _text = value; }
  10. }
  12. protected override void RenderContents(HtmlTextWriter writer)
  13. {
  14. writer.Write(_text);
  15. base.RenderContents(writer);
  16. }
  17. }

Not very interesting. Examining the above, the property takes on the customary declaration structure of:

  1. private string _property;
  2. public string Property
  3. {
  4. get { return _property; }
  5. set { _property = value; }
  6. }

Outside of the property attributes (for SharePoint interface instruction) this is 5 lines for declaration, a sizable amount to append to an arbitrary class file.

Automatic properties are instead written in the form of:

  1. public string Property { get; set; }

Taking this construct into our example class file, our WebPart code will take on the form:

  1. public class AutoWebPartClass : WebPart
  2. {
  3. [Personalizable(PersonalizationScope.Shared), WebBrowsable(true), WebDisplayName("Text To Display"), WebDescription("The Text To Display")]
  4. public string Text { get; set; }
  6. protected override void RenderContents(HtmlTextWriter writer)
  7. {
  8. writer.Write(Text);
  9. base.RenderContents(writer);
  10. }
  11. }

Much trimmer, cleaner code!

Although there is an empty setter and getter, behind the scenes the get_PropertyName and set_PropertyName methods are still being called. The compiler will generate a private field used as the backing store for the property (which is why you can’t use field attributes, but can use property attributes).

What is most important out of the use of auto-properties is the adherence to an ever important OOP concept: encapsulation! There are some things to note however, namely setting a default value and creating read-only properties.

Setting a Default Value for An Auto-Property
Seeing as WebPart properties tend to hold vital configuration values, there should be compensation for assignment of default values. There are two ways for assignment:

Set The Default Value Via Assignment In the Constructor
Set The Default Value Using DefaultValue And The Property Type

In the below, I am demonstrating the assignment of the property within the WebPart constructor:

  1. public class IntializationWebPartClass : WebPart
  2. {
  3. public IntializationWebPartClass()
  4. {
  5. Text = "Text";
  6. }
  8. [Personalizable(PersonalizationScope.Shared), WebBrowsable(true), WebDisplayName("Text To Display"), WebDescription("The Text To Display")]
  9. public string Text { get; set; }
  11. protected override void RenderContents(HtmlTextWriter writer)
  12. {
  13. writer.Write(Text);
  14. base.RenderContents(writer);
  15. }
  16. }

Creating a Read-only Automatic Property
Creating a property that is set in the class and will be immutable when used is fairly easy (however a write-only property is obviously not possible). At first glance it might be assumed that simply by removing the set of the property that the property should be read-only, however this will only create a lean on the default value. In order to create a read-property, the private access modifier simply needs to be decorated on the set of the property.

  1. public string Text { get; private set; }

That’s about all I have for now on automatic properties, because they are after all, just properties. However I hope this should get you going using auto-properties in your SharePoint code! :)


No Comments


  1. Links (9/28/2008) « Steve Pietrek - Everything SharePoint - [...] Auto-Implemented Properties and Developing WebParts [...]
  2. SharePoint Webpart Properties « PANVEGAs Blog - [...] http://www.sharepointsecurity.com/blog/sharepoint/auto-implemented-properties-and-developing-webparts/   [...]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>