How to implement a singleton class in C # Windows Forms to store data in a list

2

Implement the following classes:

  • Singleton class named AlmacenDatos with read-only properties to store the list of: Ejecutivos
  • Use the singleton class in data manipulation.


  • Add 2 objects through one of the properties of the singleton class created.
  • Display the information of the objects contained in the list The code of my executive class is the following:

  • public class ClassEjecutivo
    {
        //propiedades
        public ClassSucursal SucursalDeAdscripcion;
        public string Nombre;// { set; get; }
        public string Apellidos;// { set; get; }
        public string Domicilio; //{ set; get; }
        public string Localidad; //{ set; get; }
        public string Municipio; //{ set; get; }
        public string Estado; // { set; get; }
        public string CURP; //{ set; get; }
        public string RFC; //{ set; get; }
        public string Telefono1; //{ set; get; }
        public string Telefono2; // { set; get; }
        public string Cargo; // { set; get; }
    
    }
    

    and my other class is called DatosAplicacion , which is where I store my executive class list:

    public class DatosAplicacion
    {
       static List<ClassSucursal> listaSucursales = new List<ClassSucursal>();
    
        public static List<ClassSucursal> Sucursales()
        {
            return listaSucursales;
        }
    
        public static ClassSucursal AgregarSucursal (ClassSucursal item)
        {
            listaSucursales.Add(item);
            return item;
        }
    
        
    asked by Ángel Luis 14.09.2016 в 05:59
    source

    2 answers

    1

    If you want to use the pattern in your classes the following code can help you:

    public class classToInstance
    {
        private classToInstance() { }
        private static classToInstance _instance;
        public static classToInstance Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new classToInstance();
                return _instance;
            }
        }
    
        public void Method()
        {
            //TO DO
        }
    }
    

    Where you can use the instantiated class directly, you can use it in the following way:

    classToInstance.Instance.Method();
    

    In this way, you will always be using a single instance of the class in the code that is currently running.

        
    answered by 12.10.2016 в 02:17
    0

    Here I publish an idea that may help you.

    public class AlmacenDatos
    {
      private static AlmacenDatos _instancia;
      private List<ClassEjecutivo> ejecutivos;
    
      private AlmacenDatos()
      {
        ejecutivos = new List<ClassEjecutivo>();
      }
    
      public static getInstancia()
      {
        return _instancia?? (_instancia= new AlmacenDatos()); ;
      }
      //enviar un id único para diferenciar a cada ejecutivo
      public ClassEjecutivo getEjecutivo(string RFCparam)
      {
        foreach(ClassEjecutivo e in ejecutivos)
        {
          if (e.RFC == RFCparam)
            return e;
        }
        return null;
      }
    }
    
        
    answered by 27.10.2016 в 17:49