problem when searching a list c #

1

I have a list of books and I want to find one but at the time of doing it, it is not returning it to me.

Instance of the list:

public class Biblioteca
{
    public List<Libro> LaListaDeLibros { get; set; }

    public Biblioteca()
    {
        LaListaDeLibros = new List<Libro>();
    }

    public void AgregarLibro(Libro nuevoLibro)
    {
        LaListaDeLibros.Add(nuevoLibro);
    }

my method to obtain:

  public Libro ObtenerUnLibro(string NombreDelLibro)
    {
        Libro elLibroBuscado = new Libro();
        foreach (Libro elLibro in LaListaDeLibros)
        {
            if (elLibro.Nombre.Equals(NombreDelLibro))
            {
                elLibroBuscado = elLibro;
                return elLibroBuscado;
            }
        }
        return null;
    }

my button event:

private void btnBuscar_Click(object sender, EventArgs e)
    {
        LogicaDeNegocio.Biblioteca ElGetorDeLibros = new LogicaDeNegocio.Biblioteca();
        if (ElGetorDeLibros.VerificarLaExistenciaDeUnLibro(txtNombreLibro.Text))
        {
            MessageBox.Show("Libro encontrado", "Encontrado", MessageBoxButtons.OK, MessageBoxIcon.Information);
            txtNombrePersona.Enabled = true;
        }
        else
        {
            MessageBox.Show("El Libro no existe", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
     }

method to verify:

public Boolean VerificarLaExistenciaDeUnLibro(string nombre)
{
   if (ObtenerUnLibro(nombre) == null)
   {
      return false;
   }
      return true;
   }
}

I run it step by step and when it comes to the foreach I get the list in 0, but if you are adding well to the list, then I do not know what the error could be.

Thank you!

    
asked by jose 08.05.2018 в 20:50
source

1 answer

2

The whole logic of what you are showing is confusing and you have misconceptions.

You have a library class, which is perfect.

That class has a list of books, which is perfect. The list is initialized (at zero) during the creation of the object. All that is excellent.

Now, that's when your problems start.

private void btnBuscar_Click(object sender, EventArgs e)
{
    LogicaDeNegocio.Biblioteca ElGetorDeLibros = new LogicaDeNegocio.Biblioteca();

If we do that there, we are creating a new instance of our library. It does not sound logical, if we are looking for it, we should be in the same instance that we surely create elsewhere and already has the library loaded with books. This one you are creating here, is totally empty.

public Boolean VerificarLaExistenciaDeUnLibro(string nombre)

This method does not make any sense. it does not do anything important, except to transform a null into a boolean, which can be done in the main code.

Some things you are doing can be solved with LINQ.

Be sure to enter the corresponding library object. I do not know the rest of your code, but it may be that you do not have a global access to the library directly (I almost bet you add these by adding a new library and add them there). So add, but you're never saving the entire library!

    
answered by 08.05.2018 / 21:12
source