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!
-- Kristin
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!
-- Kristin