Click here to Skip to main content
15,891,850 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi,
I think I am getting ghosted problem in my code. I have a Class named RedCupSkinManager in Class Library projects. I have created a public instant for this class. But, in my Main Windows Application project I could't access the instance.
In my RedCupSkinManager class:
C#
namespace RedCupSkin
{
    public class RedCupSkinManager
    {
      private static RedCupSkinManager instance;
 
      public static RedCupSkinManager Instance
        {
            get { return instance ?? (instance = new RedCupSkinManager()); }
        }
    }
}


In my Main Application:
C#
using RedCupSkin;
using RedCupSkin.Controls;


namespace RedCup
{
    public partial class RedCup : RedCupForm
    {
        private readonly RedCupSkinManager redcupSkinManager; 
        public RedCup()
        {
            InitializeComponent();
            redcupSkinManager = new RedCupSkinManager.Instance;
        }
    }
}


Is there any building problem in vs-2015 or as my class is not static thus while not calling static function throw this class?
Thanks in advance.

What I have tried:

I am developing in VS-2015. I have Build RedCupSkin as its a class library and I have added the reference(RedCupSkin.dll) in my RedCup Windows application.

But, I am getting exception "the type name "instance" does not exist".
Posted
Updated 31-May-16 1:35am

Try this:
C#
namespace RedCupSkin
{
    public class RedCupSkinManager
    {
        // private 'ctor for Singleton
        private RedCupSkinManager()
        {    
        }

        private static RedCupSkinManager _instance;

        public static RedCupSkinManager Instance
        {
            private set { _instance = value; }
            
            get
            {
                if(_instance == null) _instance = new RedCupSkinManager();
                return _instance;
            }
        }
    }
}
In the Form:
C#
private RedCupSkinManager redcupSkinManager;

private void YourForm_Load(object sender, EventArgs e)
{
    redcupSkinManager = RedCupSkinManager.Instance;
}
I assume your goal here is to make the 'RedCupSkinManager a "singleton," as in the simple example shown here; there are a number of ways to do that from relatively simple to complex. See Jon Skeet's comprehensive review here: [^].
 
Share this answer
 
You don't need the "new"

C#
redcupSkinManager = RedCupSkinManager.Instance;
 
Share this answer
 
Comments
Member 10261487 31-May-16 7:32am    
Thank you. This is working.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900