Skip to content

Extension Methods

nairdo edited this page Dec 13, 2012 · 2 revisions

Rock provides several extension methods to various .NET objects. For a developer to take advantage of these, they just need to add a using to the base Rock namespace.

    using Rock;

Here are a few methods we thought we'd highlight for you (-- just enough to give you some incentive to go read the code.)

TODO - once Sandcastle is building our class reference documentation we'll add a link to the page for the Rock.ExtensionMethods class documentation.

String Extension Methods

  • Ellipsis(int) - Truncates a string after a max length and adds ellipsis. Truncation will occur at first space prior to maxLength.

  • Pluralize() - Pluralizes the specified string. In other words book becomes books and candy becomes candies.

Boolean Methods

  • ToYesNo() - Returns Yes for true and No for false.

Enum

  • ConvertToString() - Converts to the enum value to it's string value. We even split on the case changes such that an enum of RecordStatus would return "Record Status".

  • ConvertToInt() - Vice versa -- for consistency.

  • ConvertToEnum() - Converts a string value to an enum value. This one is worth showing...

    // Assuming you've got an enum such as:
    public enum Status
    {
        Success,
        Fail,
        EpicFail,
    }

    // elsewhere, you can get the proper enum if you've only got the string value:
    Status s = "Epic Fail".ConvertToEnum<Status>();    // w00t!

DropDownList/ListControl Extensions

  • BindToDefinedType( DefinedTypeCache ) - Binds a given DefinedType to your drop down ListControl. Example:
    ListControl ddl = new ListControl();
    ddl.BindToDefinedType( DefinedTypeCache.Read( myConfiguredDefinedTypeGuid ) );

Clone this wiki locally