Monday, March 16, 2015

String Extension to use instead of String.IsNullOrEmpty

Everyday while writing C# code until C# 6.0 comes I have to write code like this many times.

var firstName ="";
if(String.IsNullOrEmpty(firstName)){
"firstname is null or empty".Dump();
}

So one might ask what is wrong with this code. Nothing but it is not quite intuitive and readable. When you read, it doesn’t sound appropriate. I think that if(firstName.IsNullOrEmpty()) reads better and one doesn’t have to think of calling a static function.

public static class StringExtension
{
public static bool IsNullOrEmpty(this string value)
{
return String.IsNullOrEmpty(value);
}
public static bool IsNullOrWhitespace(this string value)
{
return String.IsNullOrWhiteSpace(value);
}
}
This allows us following.
if(firstName.IsNullOrEmpty())
{
"This reads much better.".Dump();
}

Sunday, March 1, 2015

PowerForms update - 0.3.4.16609

In the previous post, I introduced PowerForms Nuget package that allows you to create Windows Forms quickly using Fluent syntax. After publishing I have found some inconsistencies and found that certain methods are absurd. So in this post I am going to highlight what’s changed. 

How to get the update? If you are using it inside Console application then you can just install or update the package from Nuget Package Manager and using console use the following command.

Install-Package PowerForms –Version 0.3.4.16609

Changes

1. AutoSuggest method will no longer be supported in the next versions. You should be using separate overloads for doing the same. For example, If you want suggestions for TextBox then use TextBoxFor method that takes in IEnumerable<string>

  var form = new PowerForm();

var dt = form.TextBoxFor("FirstName",new string[]{"Mike","Molly","Manny","Maya"}).Display();

System.Console.WriteLine(dt["FirstName"].ToString());

2. Similarly if you now want to populate values for ComboBox then instead of using “Using” method use ComboBoxFor that takes IEnumerable<string>

 var form = new PowerForm();

var dt = form.ComboBoxFor("City",new List() { "Chicago", "Canton", "Cupcake" }).Display();

System.Console.WriteLine(dt["City"].ToString());

3. Calendar control was not obvious as how it was getting generated. So introducing CalendarFor method that will add a Calendar control.

var form = new PowerForm();

var dt = form.CalendarFor("Anniversary").Display();

System.Console.WriteLine(dt["Anniversary"].ToString());

I am very excited for PowerForms as this saves my so much of time within LinqPad. I have multiple queries that use PowerForms. It transforms LinqPad into a sophisticated tool. If you would like more features please suggest in the comments below and share how are you using PowerForms.