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!