简体   繁体   中英

C#.Net - How can i create only one object of the class and use the same through out the project

I am going to create windows form application.

In that, I want to create only one object of Commport class and make it globalize. So that I can use all the member of that class through out my project. I saw questions like this on stack. Every body suggested to use singleton pattern. I tried to create CommPort class as singleton but I am unable to understand, how to create its object or in which class or exactly where to create. And how to call its function. Any help will be appreciated.

My application contains following class.

main form:

namespace Testcertificate
{
    public partial class Testcertificate : Form
    {
        public Testcertificate()
        {
            InitializeComponent();
        }

        private void bt_Connect_Click(object sender, EventArgs e)
        {
            Connect connect = new Connect();
            connect.ShowDialog();
        }
    }
}

Second Form:

namespace Testcertificate
{
    public partial class Connect : Form
    {
        public Connect()
        {
            InitializeComponent();
        }
    }
}

I need to create two more classes

one class:

namespace Testcertificate
{
    class TxBuffer
    {
    }
}

And another one is:

namespace Testcertificate
{
    class CommPort
    {
    }
}

Did you see this explanation? It's clearly. Singleton pattern explanation by microsoft Probably it could help you.

Summary You may use one of this variants of implementation:

  1. “Classic” Singleton;
  2. Static.

Classic Singleton is:

using System;

public class Singleton
{
   private static Singleton instance;

   private Singleton() {}

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

And static is:

public sealed class Singleton
{
   private static readonly Singleton instance = new Singleton();

   private Singleton(){}

   public static Singleton Instance
   {
      get 
      {
         return instance; 
      }
   }
}

And there is how to create instance of:

class Program
{
    static void Main(string[] args)
    {
        Computer comp = new Computer();
        comp.Launch("Windows 8.1");
        Console.WriteLine(comp.OS.Name);
         
// you can’t edit OS because object already created
        comp.OS = OS.getInstance("Windows 10");
        Console.WriteLine(comp.OS.Name);
         
        Console.ReadLine();
    }
}
class Computer
{
    public OS OS { get; set; }
    public void Launch(string osName)
    {
        OS = OS.getInstance(osName);
    }
}
class OS
{
    private static OS instance;
 
    public string Name { get; private set; }
 
    protected OS(string name)
    {
        this.Name=name;
    }
 
    public static OS getInstance(string name)
    {
        if (instance == null)
            instance = new OS(name);
         return instance;
}
}

Then you have to create a singleton class like this

public sealed class Singleton
{
    Singleton()
    {
    }
    private static readonly object padlock = new object();
    private static Singleton instance = null;
    public static Singleton Instance
    {
        get
        {
            lock (padlock)
            {
                if (instance == null)
                {
                    instance = new Singleton();
                }
                return instance;
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM