• Home
  • About
  •  

    Useful Extension Method On String

    June 30th, 2008

    .NET Framework provides String.IsNullOrEmpty() method which return true if the string is null or empty. It does not however take care of a string which only contains spaces. Often while persisting data we do not want to persist just empty strings. In past I have made this check before I let my data pass through. Today I thought of creating an extension method to do the same for me. Below is the code for the extension method which I have written in a StringExtensions class.

    public static class StringExtensions
    {
      public static bool IsNullOrEmptyWhenTrimmed(this string s)
      {
        return  !(s != null && s.Trim().Length > 0) ;
      }
    }

    Here is an example on how this extension method can be used.

    string x = "  ";
     
    if (x.IsNullOrEmptyWhenTrimmed())
      Console.WriteLine("String is empty or null");
    else
      Console.WriteLine("String is NOT empty or null");

    From now on I will be using this extension method whenever I have to check for empty strings. Short and sweet and useful :)

    kick it on DotNetKicks.com


    Get Your Free Windows Vista Wallpapers

    June 30th, 2008

    I stumbled upon a site today which as 74 Windows Vista Wallpapers available for download. I cannot figure out why most of them are green. Anyhow they look good. So browse away to quench your gradient thirst.

    Vista


    Will my software work with Windows Vista?

    June 30th, 2008

    Vista Since launch of Windows Vista one of the top criteria for taking the plunge is will existing applications work under Vista. To answer this Microsoft created a web site which lists applications that will work on Vista. So if you are concerned about your favourite early-man application then visit AppReadiness and find out.


    How do you become a Successful Developer?

    June 30th, 2008

    Recently Andrew posted a response to the question "How do you become a successful developer?" This question has received many responses from the community and I am presenting my 2 cents here.

    Among all wonderful suggestions I’d like to add Stay Focused. In this day and age you are being bombarded by CTPs and Betas on daily bases. There are new technologies coming out left right and centre and it becomes hard for someone to stay focused. The issue is that most of the new technological arrivals on Microsoft stack are too damn interesting to ignore. However one can loose track here and end up spending most of their time looking at bits which may be out of their core competency. You run into the danger of getting distracted and loose your edge. So I say, stay focused on your area of specialisation and master the art as if your life depends on it.

    But the new CTPs are so cool

    Now any developer must look at the new technology which comes out. Honestly, this is probably one of the reason we choose software development as a career path. It is the excitement of working with new cool technologies and solving problems using technology which keeps us going. But a balance is required. The way I try to stay focused and still be able to learn and play with new stuff is that I follow the Google way. Yes you can learn things everywhere and the 20/80 split in time followed by Google employees seems to be a good approach. I spend at-least 20% of my time learning new technologies, playing with beta software and keeping up-to-date with the latest trends. Being a consultant I do not get paid for that 20% so it is purely my time and I value it highly.


    Goodbye Bill Gates

    June 29th, 2008

    Saying that this is an end of era will be an understatement.


    Microsoft Task Market

    June 28th, 2008

    image

    Microsoft Office Labs and Microsoft Research Asia have launched a site called Microsoft Task Market. The way it works is that if you have a task you need to get done then you can post it on Task Market and let people who can do the task contact you and bid for the piece of work. People who do the task can get paid through PayPal. The site is hardly popular at all. I actually stumbled upon it. I have never heard about it or seen any banner ads anywhere. This could be because the site is still under Tech Preview. Many other companies have creates similar sites in the past and only very few have survived. It will be interesting to watch Microsoft’s offering in this space.


    Useful Findings - SQL Server 2008 Management Studio Drop And Create

    June 26th, 2008

    Working with the new SQL Server Management Studio which will ship with SQL Server 2008, I can see some common sense boosted improvements. One of these improvements is that now you can generate a Drop and Create script in one go. Often when you generate scripts you want a Drop and Create. This can easily be done from Management Studio now.

    SSMS


    LINQ - Ordering Operators

    June 23rd, 2008

    LINQ comes packed with few useful ordering operators. These are: OrderBy, ThenBy, OrderByDescending and ThenByDescending. I will walkthrough these operators using an example of cities. Here is the code for City class.

    public class City
    {
        public string Name { get; set; }
        public string Country { get; set; }
    }

    Below is the intitializer for cities collection.

    List<City> cities =
        new List<City>
        {
            new City{ Name = "Sydney", Country = "Australia" },
            new City{ Name = "New York", Country = "USA" },
            new City{ Name = "Paris", Country = "France" },
            new City{ Name = "Milan", Country = "Spain" },
            new City{ Name = "Melbourne", Country = "Australia" },
            new City{ Name = "Auckland", Country = "New Zealand" },
            new City{ Name = "Tokyo", Country = "Japan" },
            new City{ Name = "New Delhi", Country = "India" },
            new City{ Name = "Hobart", Country = "Australia" }
        };

    I will now walk you through ordering operators. First one we will look at is the OrderBy operator. OrderBy as the name implies orders the collection with a specified field in ascending order. To order our collection by country we can use the following code.

    var result =
        from c in cities
        orderby c.Country
        select c;

    Using the loop below I can see that my collection has been ordered by country.

    foreach (var item in result)
    {
        Console.WriteLine(
            string.Format("Name: {0}\t Country: {1}", item.Name, item.Country));
    }

    And here is the output.

    ThenBy operator can be used to order an already ordered collection using another criteria. For example we can use it to order our list by Country and then by Name.

    var result =
        cities
        .OrderBy(c => c.Country)
        .ThenBy(c => c.Name);

    I can also use ThenBy explicitly in my OrderBy like this.

    var result =
        from c in cities
        orderby c.Country, c.Name
        select c;

    OrderByDescending orders the collection in descending manner. This can be accomplishes by simply using the descending keyword.

    var result =
        from c in cities
        orderby c.Country descending
        select c;

    ThenByDescending orders an already ordered collection using another criteria but this time in descending manner.

    Explicit use of ThenByDescending:

    var result =
        cities
        .OrderBy(c => c.Country)
        .ThenByDescending(c => c.Name);

    Implicit use of ThenByDescending:

    var result =
        from c in cities
        orderby c.Country, c.Name descending 
        select c;

    By using ThenByDescending operator explicitly or implicitly we get the same output.

    Ordering is just one category of standard LINQ query operators. The more I work with LINQ, the more I find out how interesting and useful LINQ is. Stay tuned for more posts on LINQ.

    Technorati Tags:

    kick it on DotNetKicks.com


    LINQ - Where Operator

    June 21st, 2008

    “Where” operator is the most commonly used LINQ operator. For those familiar with SQL “Where” operator will be obvious. It filters the results based on specified criteria. Below is an example using a list of cities.

    List<string> cities =
      new List<string> { 
        “New York”, 
        “Sydney”, 
        “Paris”, 
        “New Delhi” };
    
    
     
    var result = 
      from c in cities
      where c.StartsWith(“New”)
      select c;
     
    foreach (string item in result)
    {
      Console.WriteLine(item);
    }

    Here we used the where operator to select cities which start with “New”. Below is the output:

    image

    Technorati Tags:


    Cleared 70-502 WPF Exam Today

    June 5th, 2008

    MCTS(rgb)_1098

    I passed Exam 70-502 Microsoft .NET Framework 3.5 - Windows Presentation Foundation Application Development today with a score of 984. I think I got one question wrong related to data binding. This makes me a MCTS: .NET Framework 3.5 - Windows Presentation Foundation Applications.

    The overall exam is not too difficult if you are already working with WPF. As an advise to anyone who wishes to attempt 70-502 I will say that get comfortable with XAML. The exam is loaded with it and most of the questions where you have to read code are in XAML. And read the questions very carefully.

    It’s time for me to celebrate now. Its too early for a beer, so I’ll settle for a delicious caffeinated beverage.