Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Singleton Pattern in iComparer Interface Class

Status
Not open for further replies.

RileyCat

Programmer
Apr 5, 2004
124
0
0
US
I am writing a C# Windows app that will contain more than one ListView control. Because one form will contain more than one ListView control, I want to try to use the Singleton pattern on the implementation of my "iComparer" interface class. I am developing in VS2003

The code compiles nicely, but at run time, I get the error of "Object Is Not Set To Instance Of An Object".

Is there anything different I need to be doing to assign the singleton instance of my "iComparer" class to the ListViewItemSorter?

Or, Is there some reason why I cannot make this a singleton and if so, how else can I define the "iComparer" class so that there is only one instance alive throughout the life of the application.

I am attaching code files for reference, but here is some code snippets, too .......

From "Form1.CS"

ListViewColumnSorter.GetInstance() ;
this.listBox1.ListViewItemSorter = (IComparer)(ListViewColumnSorter.GetInstance()) ;

From "ListViewColumnSorter.cs"

public sealed class ListViewColumnSorter : IComparer
{
/// <summary>
/// ALBREK01 :: Define static singleton instance of class
/// </summary>
private static readonly ListViewColumnSorter instance = new ListViewColumnSorter() ;

// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
// ALBREK01 :: Class constructor
static ListViewColumnSorter()
{
InitColumnSorter () ;
}

// ALBREK01 :: Public access to singleton class instance
public static ListViewColumnSorter GetInstance()
{
return (instance) ;
}
}


Stay Cool Ya'll! [smile2]

-- Kristin
 
Kristin,

I have to wonder why you chose a singleton for this class. The danger is if you have two different listviews and one of them needs to be sorted differently than the other. If 2 listviews require the same columnsorter then I would create a controller to work with both of those listviews directly.

By declaring a singleton - you can only ever have one instance in your application.

For the sake of a singleton, I have modified your code. Some may argue about this implementation but I have never had an issue with it.

The usage now changes too. What was:

Code:
ListViewColumnSorter.GetInstance() ;
this.listBox1.ListViewItemSorter = (IComparer)(ListViewColumnSorter.GetInstance()) ;

is now

Code:
this.listBox1.ListViewItemSorter = ListViewColumnSorter.GetInstance();

You should not need to cast in this case because your ListViewColumnSorter implements the IComparer interface.


Code:
public sealed class ListViewColumnSorter : IComparer
{
    private static ListViewColumnSorter instance = null;

   static ListViewColumnSorter()
   {
      InitColumnSorter () ;
   }

   public static ListViewColumnSorter Instance
   {
      get
      {
         if (instance == null)
         {
            instance = new ListViewColumnSorter();
         }

         return instance;
       }
   }
}

 
Agreed.

Ask the simple question......do I need to guarantee the instance only exists once?

The answer is no.

Hence no need for singleton.

C
 
Okay this is great! My next question is .... Do I need to destruct the instance of the comparer class as the form closes? Or does form dispose do that for me?

Stay Cool Ya'll! [smile2]

-- Kristin
 
Depends!

Personally, I wouldn't. I could happily wait for it to be disposed by the form and garbage collected. Others might think otherwise.

C
 
You really only need to write a destructor if you are working with unmanaged memory such as images.

If you are concerned about memory issues you can implement the IDisposable interface.

 
Download FxCop, or run CodeAnalysis (if you have the Team Developer edition). It'll tell you if you're missing an IDisposable implementation anywhere.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top