C# Tips n Tricks: Adding a "From Camel Case" string extension method

On occasion I am working with strings that are simply the enum value, such as “MyEnum.SampleEnumName”.  I want to display it as a string without the camelcase formatting “Sample Enum Name”.  To do this I added the following FromCamelCase extension method to string.

namespace CustomExtensions
{
    public static class StringExtension
    {
        public static string ToStringOrEmpty(this object value)
        {
            return string.Concat(value, "");
        }

        /// <summary>
        /// Converts a string from CamelCase to a human readable format. 
        /// Inserts spaces between upper and lower case letters. 
        /// Also strips the leading "_" character, if it exists.
        /// </summary>
        /// <param name="propertyName"></param>
        /// <returns>A human readable string.</returns>
        public static string FromCamelCase(this string propertyName)
        {
            string returnValue = null;
            returnValue = propertyName.ToStringOrEmpty();

            //Strip leading "_" character
            returnValue = Regex.Replace(returnValue, "^_", "").Trim(); 
            //Add a space between each lower case character and upper case character
            returnValue = Regex.Replace(returnValue, "([a-z])([A-Z])", "$1 $2").Trim(); 
            //Add a space between 2 upper case characters when the second one is followed by a lower space character
            returnValue = Regex.Replace(returnValue, "([A-Z])([A-Z][a-z])", "$1 $2").Trim(); 

            return returnValue;
        }
    }
}

C# Tips n Tricks: Coalesce

I’ve been using SQL for many years and find COALESCE to be extremely useful.  Recently I realized that if I could do the same functionality in C# it would be helpful, so I started to look.

For basic Coalesce functionality all you need to do is use the ?? operator.

string name = nameA ?? nameB ?? nameC ?? string.Empty;

For more advanced functionality I am planning to use the logic I found in the “Extending the C# Coalesce Operator” post on StackOverflow  to create an extension method that will allow the following code to work.

public class Bar
{
  public Bar Child { get; set; }
  public Foo Foo { get; set; }
}

Bar bar=new Bar { Child=new Bar { Foo=new Foo("value") } };

// prints "value":
Console.WriteLine(Coalesce.UntilNull(bar, b => b.Child, b => b.Foo, f => f.Value) ?? "null");

// prints "null":
Console.WriteLine(Coalesce.UntilNull(bar, b => b.Foo, f => f.Value) ?? "null");

ReportViewer: Alternating color on rows

According to MSDN you do the following:

Creating a Green-Bar Report To apply a green-bar effect (alternating colors every other row) to a table in a report, use the following expression in the BackgroundColor property of each text box in the detail row:

=iif(RowNumber(Nothing) Mod 2, "PaleGreen", "White")

To alternate colors by row groupings:

=iif(RunningValue({Expression Being Grouped On}, CountDistinct, nothing) Mod 2, "WhiteSmoke", "White")

JavaScript Tips n Tricks: javascript:void(0)

Two options

<a href="#" onclick="myJsFunc(); return false;">Run JavaScript Code</a>

or

<a href="javascript:void(0)" onclick="myJsFunc();">Run JavaScript Code</a>

Both options do almost the same thing.  By using the second one you will not need to remember to use the “return false;” that is in the first one.  When writing large amounts of complex code using this pattern saves on bugs and time.  One issue with it is accessibility, I will need to do more research on this when the time comes.

StackOverflow question : Href attribute for JavaScript links: “#” or “javascript:void(0)”?

C# Tips and Tricks: Checking for empty strings

When checking to see if a string is empty I got tired of checking for null and then seeing if it was empty now I only use the following method.

public string StandardMethodName(string inputString)
{
 if(string.IsNullOrEmpty(inputString))
 {
  inputString = "Default value";
 }

 return inputString;
}