I have an ArrayList of objects of a class but I don't know how to access the information of the ArrayList.
class Personas
{
string nombres ="";
string apellidos ="";
public Personas(string nombres,string apellidos)
{
this.nombres = nombres;
this.apellidos = apellidos;
}
}
private void button3_Click(object sender, EventArgs e)
{
Personas personas;
ArrayList lista;
Personas personas = new Personas("BYRON","RAMIREZ");
lista = new ArrayList();
lista.Add(personas);
MessageBox.Show(personas[0]["nombre"]);
}
I get an error when displaying the information.
One option is to overwrite the method
ToString()
:this way you could print the value of the object:
Another option is to add the modifiers
public
to your object's properties:This way you could read the properties:
There are several things wrong and to take into account in your code:
Of the class
Personas
It is not a good practice to name a class that is not a collection in plural, the same for the fields, they must also be public or you will not be able to access them outside the class. Modify it to something like this:
The ArrayList gives you an error because when you access element 0 of its position, it returns the object of that position, that is, a Person element, in order to correctly access the name field that you indicate, you must do the following: