Friday, August 28, 2009

.NET Tips and Tricks [Extension Classes]

Characteristics of extension classes:
- Must be public
- Static functions
- Include keyword "this" in the parameter to be extended
- Solution must include the reference System.Core
- Class must be in same namespace

Example of use:
namespace ReportingWebApp
{
public static class SPListItemExtensions // extends the SPListItem class
{
public static string GetStringValue(this SPListItem item, string fieldName)
{
// insert code here
}
}
}

In main application:
SPListItem item = myList.Items[0];
string itemStringValue = item.GetStringValue("Color");

No comments:

Post a Comment