22 November 2011

Be Careful of Constant fields in .NET

We all know that Constant fields are those whose value never changes in the lifetime of an application. Constant are really useful constructs that offer many benifits to the user like,
  • Constants make developer sure that their value never changes (even by mistake).
  • Constants are compile time values, which means their values are directly embedded inside the metadata. Hence, constants don't require memory allocation at runtime.
All that said, there is a risk in using the constants if appropriate care is not taken. See the second point above - "constant values are embedded directly inside the metadata". This statement has an impact when you have constant fields in a DLL which are used in one or more applications. Let me start demostrating you through a sample code. Consider that the below class exists in a library called MyLibrary.DLL.


public class GlobalConstants
{
    public const Int32 MAX_VALUE = 2;
    public const String MALESTRING = "Male";
    public const String FEMALESTRING = "Female";
}


Now, I have to use GlobalConstants in my application. For that I will create a Console Application called SampleClient.exe. Inside the Main function I use the above constants as


class Program
{
    static void Main(string[] args)
    {
        int maxValue = GlobalConstants.MAX_VALUE;
        String Female = GlobalConstants.FEMALESTRING;

        Console.WriteLine(maxValue);
        Console.WriteLine(Female);
    }
}


Build SampleClient.exe and run it directly from bin folder. Your application will print 2  and Female as expected and everything is fine. Now I got a requirment that MAX_VALUE should be changed to 100, MALE should be "Man" and FEMALE should change to "Woman". Ok, thats fine. I will update GlobalConstants in MyLibrary.DLL class as below.


public class GlobalConstants
{
    public const Int32 MAX_VALUE = 100;
    public const String MALESTRING = "Man";
    public const String FEMALESTRING = "Woman";
}


I will re-build MyLibrary.DLL and send the dll for use in SampleClient.exe. So, place the new dll in bin folder of SampleClient and run it. SampleClient is now refering to new MyLibrary.DLL. But what output do you see? you expect that it will print 100 and Woman but it still prints 2 and Female. So, what is the problem? For that you need to see the IL code of Main method in SampleClient.



Observe that the values 2 and FEMALE are hardcoded in the IL. Though you have changed MAX_VALUE to 100 & FEMALE to "Woman", the values are not reflected. To correct this problem, you must re-build SampleClient application too to make use of new constant value in the dll. Rebuilding SampleClient will embed the new MAX_VALUE and FEMALE values into metadata of Main method.

With this problem, consider your DLL is consumed by Hundreds of applications. So, changing a constant value in your library will result in rebuilding of all it's consumers. So, be careful with constants, use them only when you are sure that their value never-ever change or if they change, you afford re-building it's consumers. Or, if you think constant values may change, then prefer using 'readonly' fields over constants. I will discuss about 'readonly' fields in my next writing.

15 November 2011

Creating Multi Column ComboBox in .NET

Recently I was working on an assignment wherein I had to create a Custom ComboBox to display multiple columns when it is dropped down. Something similar to below image.




The actual assignmet was little more complex than the one I have shown in the above image. Anyways, if you look at the image, you will also find the combo box pretty useful as it can display data in column manner like FirstName-LastName, Name-Place etc. So, if you like it, you can design it yourself. All you have to do is, create class that inherits ComboBox, set the DrawMode property to OwnerDrawFixed, override OnDrawItem method to draw the columns manually.

Sounds simple, isn't it? Now, how do we supply items to this ComboBox? If we have to display an item in multicolumn fashion, ideally the item must be composite one i.e. a Dictionary<T, T> or a class with 2 instance members. Since ComboBox's Items property is a collection of objects, we can place anything into it. This has got both advantage as well as disadvantages.   Advantage is we can place object of our custom class into ComboBox while the disadvantage is user can enter any kind of item which cannot be represented in the way we need to display it.

Ok, all that said, I will create a class whose instances we are going to place in the combo box. I will call this class as XComboItem (X stands for Extended).


public class XComboItem
{
    public String DisplayName { get; set; }
    public Object Value { get; set; }
    public String Description { get; set; }
}


So, this is the class, whose objects we are going to place in out custom combo Box. I will name the combo as XComboBox.


public partial class XComboBox : ComboBox
{
    private Int32 ColumnGap = 20;
    private Int32 firstColumnWidth;
    private Int32 secondColumnWidth;
    public XComboBox(): base()
    {
        DrawMode = DrawMode.OwnerDrawFixed;
        firstColumnWidth = DropDownWidth / 2;
        secondColumnWidth = DropDownWidth / 2;
    }
}


In this, firstColumnWidth and secondColumnWidth are the widths of both columns that I am going to display in combo dropdown list (in pixels). Column gap is gap between two columns.Initially I will set the column widths to half the width of DropDownWidth. The only major thing is to override OnDrawItem() function. By overriding this function, you can draw the items in DropDownList of the combobox in the way you want. You can display each item with different font, differen style or even add image to each item. In our case, we need to display DisplayItem & Description of XComboItem in two seperate columns. The logic is getting the drawing object of current item, then use DrawString method appropriately. So, here goes our OnDrawItem.

protected override void OnDrawItem(DrawItemEventArgs e)
{
    if (e.Index < 0)
    {
        return;
    }

    XComboItem item = (XComboItem)Items[e.Index];
    ColumnGap = firstColumnWidth == 0 ? 0 : ColumnGap;
    e.DrawBackground();
    e.DrawFocusRectangle();
    String first = item.DisplayName;
    String second = item.Description;

    while (TextRenderer.MeasureText(first, e.Font).Width > firstColumnWidth)
    {
        first = first.Substring(0, first.Length - 1);
    }

    Brush solidBrush = new SolidBrush(e.ForeColor);
    e.Graphics.DrawString(first, e.Font, solidBrush, e.Bounds.Left, e.Bounds.Top);
    e.Graphics.DrawString(second, e.Font, solidBrush, e.Bounds.Left + firstColumnWidth +
                          ColumnGap, e.Bounds.Top);
}


You can add any properties or methods to make the control more user friendly. So, That's it, our control is ready. To use, create the object of XComboItem and add the object to Items collection of XComboBox. Then run your application to see the effect.

4 September 2011

Built-in General Purpose Delegates in .NET

In day-to-day coding, we often need to create delegates. Every time we need a delegate, we usually create a new delegate and use it. But, if you know .NET already provides some ready made delegates which we can use for varying purposes. So, in this post I will be discussing some of the common built-in delegates that we can use for general purpose.

System.Action: The Action delegate can accept any method which takes zero arguments and returns void. The declaration goes like

public delegate void Action();
System.Action<…> : This is a generic version of System.Action delegate. This delegate can accept any method that takes 1 to 16 parameters and returns void. You can use this delegate as below,

public void SomeMethod(int a, string b, double c){}
System.Action<int, string , double> action = new System.Action<int, string , double>( SomeMethod );

System.Comparison<T>: Actually this delegate is used for specific purpose, I am discussing this here because it is often used in Sorting techniques.  This takes a method as parameter which takes two arguments of type T and return an Integer. For example, I want to sort list of employees based on their first names then I can do it as,

private int CompareEmpoyees(Employee a, Employee b)
{
     return a.FirstName.CompareTo(b.FirstName);
}

ListempList = GetEmployeeList();
Comparison comparer = new Comparison(CompareEmpoyees);
empList.Sort(comparer);


System.Func<…, out TOut> : This is a generic delegate that accepts any method that takes 0 to 16 arguments of any type and return an argument of type TOut. For example,

public Employee SomeMethod(int a, string b, double c)
{
}

System.Action<int, string , double> action = new System.Action<int, string , double, Employee>( SomeMethod );

System.MethodInvoker: This delegate is similar to System.Action delegate which accepts a method that takes zero arguments and returns void. But, as the name suggests, this delegate is used at places where you need to talk to UI elements from a background thread. Something like below,

MethodInvoker mi = new MethodInvoker ( () => { textBox1.Text = "Hello World"; });
this.Invoke(mi);


That is it for now. I will be editing this page if I find any other delegates that fit here. Meanwhile, If you want me to add anything here, please let me know.

2 July 2011

Closing all open forms in .NET

In your application you may have many hidden forms which you must close before exiting the application. It's because, hidden forms always stay in the memory until you close them. But, .net framework always takes care of disposing off all those objects when the application is terminated. But, the real problem comes when those hidden forms contain COM/ActiveX components. You all know, you must manually clean all unmanaged components. Now, if you forget to close such forms, it may lead to memory leaks.

It's very simple to close all such open forms. Application.OpenForms is the collection that contains all open forms. So, just loop through this collection and close one by one. Below is the code, where I am closing all open forms in main form's FormClosing event handler.


    private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        foreach (Form openForm in Application.OpenForms)
        {
            openForm.Close();
        }
    }

On first look, this code looks to be fine. But, there are two potential problems with this code.
First,Application.OpenForms contains your main form because it is also open currently. So, when the loop executes, it will call Close() on MainForm, which in-turn will fire FormClosing event of MainForm. This will results in calling MainForm_FormClosing method recursively and infinitely which leads to deadlock. So, a StackOverFlowException will occur. To avoid this, you can add a check as below.


    private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        foreach (Form openForm in Application.OpenForms)
        {
            if (openForm != this)
            {
                openForm.Close();
            }
        }
    }

simple, isn't it? Is it good to go now? No. Still there is a problem. To understand the problem, let us consider, we have 3 open forms. So, Application.OpenForms collection contains 3 items (say in following order)

   1. MainForm
   2. SubForm1
   3. SubForm2

Ok. The loop executes first time and skips the Close() since the condition fails. During second loop, it closes the SubForm1 and imporatantly SubForm1 is removed from Application.OpenForms collection. This means the collection is altered. Now, during 3rd loop, it tries to access the collection, an 'InvalidOperationException' is thrown saying "Collection was modified; enumeration operation may not execute". Hence, what you can do is instead of directly accessing Application.OpenForms, store all open forms in an array or a list. Then loop through the array/List and close the forms. So, below is the final code.


    private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        List<Form> lstOpenForms = Application.OpenForms.OfType<Form>().ToList();

        foreach (Form openForm in lstOpenForms)
        {
            if (openForm != this)
            {
                openForm.Close();
            }
        }
    }


I hope this information was useful.