How to access a "private static ListT" list from another class?

0

I have this code:

public static class GenDAO<T> where T : IEntidad // antes static
{
    private static List<T> BDenMemoria = new List<T>();
    private static int NextId = 1;

    public static int Crear(T entidad)
    {
        entidad.id = NextId++;
        BDenMemoria.Add(entidad);
        return entidad.id;
    }

    public static T Buscar(int id)
    {
        return BDenMemoria.FirstOrDefault(x => x.id == id);
    }

    public static void Actualizar(T entidad)
    {
        int indice = BDenMemoria.FindIndex(x => x.id == entidad.id);
        if (indice != -1)
            BDenMemoria[indice] = entidad;
    }

    public static void Eliminar(int id)
    {
        int indice = BDenMemoria.FindIndex(x => x.id == id);
        if (indice != -1)
            BDenMemoria.RemoveAt(indice);
    }
}

It is part of a code for windows forms and I need to access from the main class to the list BDenMemoria that is private and static, to show in a DataGridView the objects that the list has. But I do not know how to access that list from the outside. Thanks!

    
asked by dobarqueiro 23.07.2018 в 17:46
source

3 answers

0

I would think that it is enough to make it public and add an instance of your class in the class where you want to use it. For example:

GenDAO ejemplo = new GENDAO();

And then accessing the property using:

var lista = ejemplo.BDenMemoria;

Private variables are called like that because they belong to that specific class and it should not be possible to access them from the outside.

I hope you serve, greetings!

    
answered by 23.07.2018 в 19:34
0

In the example you have set, the BDenMemory List is static .

The following definition appears in the Microsoft documentation :

The static member is invoked in a class, even if no instance of the class has been created. You always have access to the static member with the class name, not the instance name. There is only one copy of a static member, regardless of the number of instances of the class that are created.

So access to the List is done directly without instantiating the class.

GenDAO.BDenMemoria;

If you can not change the private access switch, you can add a wrapper method.

    
answered by 23.07.2018 в 20:12
0

A member private is only accessible through a member of the same class as explained in the documentation :

  

Private access is the least permissive level of access. Private members are only accessible within the body of the class or struct in which they are declared.

The only way to directly access the list would be with at least these two characteristics:

  • Class not is static.
  • The access modifier in the list is protected .
  • An indirect way to get the list would be through an iteration with the Buscar method:

    IList<IEntidad> BDenMemoria = new List<ClaseEntidad>();
    int i = 0;
    bool stop = false;
    do
    {
        IEntidad obj = GenDAO.Buscar(i);
        if (EqualityComparer<ClaseEntidad>.Default.Equals(obj, default(ClaseEntidad)))
        {
            stop = true;
        }
        else
        {
            BDenMemoria.Add(obj);
            i++;
        }
    }
    while (!stop);
    
        
    answered by 23.07.2018 в 22:04