Estoy utilizando una lista para guardar objetos, cada uno con sus propiedades. Mi problema es que no puedo acceder a las propiedades de los objetos de esa lista para editarlas programáticamente. Mi clase Program
se ve así:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TextosApp
{
class Program
{
static void Main(string[] args)
{
//Inicializa un Anotador
Anotador anotador = new Anotador();
//La interfaz inicia y pregunta que hacer
Console.WriteLine("Qué desea hacer? Tipee 'ayuda' para más información.");
Console.WriteLine("Inserte un comando: ");
Console.WriteLine("");
bool salir = true;
//Loop del programa
while (salir)
{
string input = Console.ReadLine();
if (input == "agregar")
{
anotador.AgregarTexto();
Console.WriteLine("");
Console.WriteLine("Texto agregado con éxito.");
Console.WriteLine("");
Console.WriteLine("Inserte un comando:");
Console.WriteLine("");
}
else if (input == "listar")
{
Console.WriteLine("");
anotador.ListarTextos();
Console.WriteLine("");
Console.WriteLine("Inserte un comando: ");
Console.WriteLine("");
}
else if (input == "ayuda")
{
Console.WriteLine("");
Console.WriteLine("Los comandos disponibles son: agregar - listar - ayuda - salir");
Console.WriteLine("");
}
else if (input == "editar")
{
Console.WriteLine("");
Console.WriteLine("Inserte el Id/Numero de texto a editar");
Console.WriteLine("");
int id = Int32.Parse(Console.ReadLine());
anotador.EditarTexto(id);
}
else if (input == "salir")
{
salir = false;
}
else
{
Console.WriteLine("");
Console.WriteLine("Inserte un comando válido: ");
Console.WriteLine("");
}
}
}
}
}
Cuando ejecuto el comando editar
, paso el parámetro Id
al método correspondiente, que realiza lo siguiente:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TextosApp
{
class Anotador
{
public Anotador()
{
textos = new List<object>();
}
public void AgregarTexto()
{
Console.WriteLine("");
Console.WriteLine("Inserte un nombre para su texto:");
Console.WriteLine("");
textos.Add(new Texto()
{
Id = textos.Count,
Leido = false,
Nombre = Console.ReadLine()
});
}
public void ListarTextos()
{
if (textos.Count > 0)
{
foreach (Texto texto in textos)
{
Console.WriteLine("");
Console.WriteLine("Número: " + texto.Id);
Console.WriteLine("Nombre del texto: " + texto.Nombre);
Console.WriteLine(texto.Leido ? "(Has leido este texto.)" : "(No has leido este texto.)");
Console.WriteLine("");
}
}
else
{
Console.WriteLine("No hay textos!");
}
}
public void EditarTexto(int id)
{
Console.WriteLine("");
Console.WriteLine("Introduzca el nuevo texto: ");
Console.WriteLine("");
string nuevoTexto = Console.ReadLine();
textos[id].Nombre = nuevoTexto;
}
public List<object> textos;
}
}
Sin embargo es allí donde tengo problemas porque no puedo acceder a Nombre
. Sí puedo acceder a textos[id]
, pero no a sus propiedades. Mi clase Texto
se ve así:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TextosApp
{
class Texto //Clase simple
{
public int Id { get; set; }
public string Nombre { get; set; }
public bool Leido { get; set; }
}
}