Sunday, November 15, 2009

When to use F#.NET and C#.NET/VB.NET

F# is a new development language that is available on Visual Studio 2010; It can be downloaded separately from Microsoft research F# for Visual Studio 2008. It is a new language that has paradigm of functional programming. It is differs from C# or VB that has paradigm of imperative by design.

Functional means preventing state and mutable data, and treats as mathematic functional.  This is in contrast of imperative that can change state using statement.

In a software development project, F# is use on financial or math. Some banking applications for example needs financial project like counting interest, pmt, value manipulation or else. C#/VB is useful on UI interaction, data/io interaction.

Make IEnumerable<T> to BindingList<T>as datasource for collection control

IEnumerable<T> can be used as datasource for some controls especially on collection thing like gridview or else. It happens when assigning Datasource property from the control with data in it.

for example:

IEnumerable<Sales> sales = GetAllSales(); // return value from GetSales is IEnumerable<Sales>
GridView1.DataSource = sales;
GridView1.DataBind();

The samples above will display all sales from methods. This is good when there is no paging in GridView. For advanced use like paging or else; Try to cast or convert to BindingList<T> so it can be use in the control.

public static BindingList<T> ToBindingList<T>(this IEnumerable<T> data)
{
    BindingList<T> result = null;
    if (data != null)
    {
        result = new BindingList<T>();
        foreach (T item in data)
        {
            result.Add(item);
        }
    }
    return result;
}

The method above will help to convert to BindingList<T> and ready to use as datasource in control