• Get the number of days in a month in .NET

    If you've ever wondered how to get the number of days for a given month in .NET, the solution is pretty easy:

    System.DateTime.DaysInMonth(int year, int month)

    Hope it helps!

  • Execute javascript code every time a form is loaded via Ajax.BeginForm

    If you've ever used the Ajax.BeginForm code to render a form in ASP MVC using Ajax (via jQuery, for example), you may have wondered there's an object you can pass to the call called AjaxOptions. This object allows you to pass the BeginForm call some interesting options to further enhance the form when it's rendered. One of those options is specially useful if you want some javascript to be executed just after the form has been rendered. You can always use the $(document).ready in jQuery, but this code will not be called if, for example, the form is rendered after a postback and you have some validation errors (a missing required field, for example). Fortunately, you can use the AjaxOptions object to tell the form to execute a method right after the form has been loaded (for the first time or after a postback).

    Read on →

  • Creating and consuming JSON data in MVC 2

    Recently I started working in a project using MVC 2. I found out a very useful feature to retrieve JSON data from the server using an Action in a Controller.

    This feature is in the base Controller class, which we inherit when we create a new controller. If you take a look into the different methods this class has you'll find those ones:

    // Summary:
    //     Creates a System.Web.Mvc.JsonResult object that serializes the specified
    //     object to JavaScript Object Notation (JSON).
    // Parameters:
    //   data:
    //     The JavaScript object graph to serialize.
    // Returns:
    //     The JSON result object that serializes the specified object to JSON format.
    //     The result object that is prepared by this method is written to the response
    //     by the MVC framework when the object is executed.
    protected internal JsonResult Json(object data);

    Along with this method, there are some overloads that allow more parameters, you can see them all in here: Controller.Json Method. In this example I'll use the first one, which is the simplest.

    Read on →

  • C#, the "and" operators and lazy evaluation

    Today at work we found a bug. My workmate, not used to C#, usually uses the & operator to compare boolean values. However, in C#, the & operator does not use lazy evaluation.

    One curious thing about C# is that it can use two different operators to calculate an and expression: the & operator and the && operator. The difference between both is that the first one (&) can be used both with integer types and boolean types. When used with integer types it will perform a bitwise comparison between the two, and when used with boolean values it will use the logical and operation between the two boolean values, evaluating all the parts of the expression. This means that using a code like this one:

    if (someObject != null & someObject.SomeProperty == someValue)

    will throw a runtime error if someObject is null, because it will try to obtain the SomeProperty value.

    However, the && operator is only available to boolean expressions, and it uses lazy evaluation, this is, if the first condition evaluated is false, it will calculate false without evaluating the rest of the expression, because an and is only true if all the expressions are true.

    Conclusion, be sure to always use && when evaluating boolean values if you want to avoid run time surprises :).

  • Creating and testing a Linked List based Queue in C# using Nunit

    The Queue<T> class (and Stack<T> too) of the .NET Framework from Microsoft is implemented using an array. While this is a perfectly good approach, I think that a Linked List based implementation could be desired in some situations (specifically when the size of the queue is not fixed).

    Since the implementation alone would be rather simple for a post, I’ll show you how to implement Unit Testing with the class using Nunit. Although this is a rather simple class to test I think it will show the basic concepts behind unit testing.

    Read on →