Reputation: 239
I am struggling with using Singleton design pattern. I am trying to use it in this simple console application. I have a problem with it in the Main method in Program class. I want to define object from the Singleton class such as: var data = Singleton.Instance;
but I don't know why I can't do that
Also, I don't know why I am getting the following error message when I run the program:
Unhandled Exception: System.NullRefernceException: Object reference not
set to an instance of an object.
So how to fix that?
Singleton Class:
namespace Singleton
{
class Singleton
{
//Variable
private static Singleton instance;
private List<string> Messages;
//Constructor
private Singleton() { }
//Property
public static Singleton Instance
{
get
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
//Methods
public void Message(string message)
{
Messages.Add(message);
}
public bool HasMessage(string message)
{
return Messages.Contains(message);
}
}
}
Program Class:
namespace Singleton
{
class Program
{
static void Main(string[] args)
{
var data = Singleton.Instance;
Singleton.Instance.Message("Hello World!!!");
if(Singleton.Instance.HasMessage("12"))
Console.WriteLine("NO STRING!!!");
else
Console.WriteLine("There is a match");
}
}
}
UPDATE:
Guys, I really appreciate your help so far. The program is working now but the logic is not working. If you look at the main program, you will see that the list only has "Hello World!!!". However, when I used the HasMessage method doesn't work. Because the program keeps showing "There is a match". But it should show me "NO STRING!!!" as there is no match. So how to fix that?
Upvotes: 9
Views: 25044
Reputation: 209
That's the implementation of Singleton:
public sealed class SingletonExample
{
//static Field
private static SingletonExample _seInstance = null;
private int _nCounter = 0;
// private constructor
private SingletonExample() { _nCounter = 1; }
//public static get(), with creating only one instance EVER
public static SingletonExample SeInstance
{
get { return _seInstance ?? (_seInstance = new SingletonExample()); }
}
}
How to invoke and create an instance?
SingletonExample si1 = SingletonExample.SeInstance;
SingletonExample si2 = SingletonExample.SeInstance; // it will be the same object
System.Diagnostics.Debug.WriteLine(si1.Equals(si2));// TRUE
Upvotes: 0
Reputation: 43036
However, when I used the HasMessage method doesn't work. Because the program keeps showing "There is a match". But it should show me "NO STRING!!!" as there is no match. So how to fix that?
This should really be a separate question, but I'll answer it anyway. You've got your condition backwards. Your code is says to write "no string" if the instance does contain the message "12", and "There is a match" if it does not. Try this:
if(Singleton.Instance.HasMessage("12"))
Console.WriteLine("There is a match");
else
Console.WriteLine("NO STRING!!!");
Upvotes: 4
Reputation: 223187
Your field Messages
is not initialized to anything. That is why you are getting the exception. In your class do:
private List<string> Messages = new List<string>();
You may also look at Thread Safe Singleton implementation by Jon Skeet
EDIT:
Based on the updated question. Your Check and Message are opposite. It should be:
if (Singleton.Instance.HasMessage("12"))
Console.WriteLine("There is a match");
else
Console.WriteLine("NO STRING!!!");
Your method HasMessage
returns true if the passed parameter is present in the list and false otherwise.
Upvotes: 12
Reputation: 1027
It looks like you're almost there. Consider rewriting your code like this:
class Singleton
{
//Variable
private static Singleton Instance;
private List<string> Messages;
//Constructor
private Singleton()
{
Messages = new List<string>(); //Make sure to instantiate instance types before use.
}
//Property
public static Singleton GetInstance()
{
if (Instance == null)
{
Instance = new Singleton();
}
return Instance;
}
//Methods
public void Message(string message)
{
Messages.Add(message);
}
public bool HasMessage(string message)
{
return Messages.Contains(message);
}
}
There are some helpful C# tutorials for design patterns on this site.
Upvotes: 4
Reputation: 23675
private List<String> Messages;
Here is your problem. The member has never been instanciated in your code. You could do as follows:
//Constructor
private Singleton()
{
Messages = new List<string>();
}
Also i suggest you to use proper naming conventions for your local variables and members. Change instance
to m_Instance
and Messages
to m_Messages
. Also try to implement singletons in a thread-safe way... for more informations look at this page.
Upvotes: 1