How to declare a list of objects and then assign data? C #

3

I tried to find information about it and found that I can declare the list as follows:

var list= new List"object"; 

but I do not take well the different types of syntax I find from examples on the Internet.

How should I declare the list?

using System;
using System.Collections;

namespace TP
{

    public class AltaDeProducto
    {


        public AltaDeProducto()
        {
                //EN ESTE CONSTRUCTOR DEBERIA TENER EL LIST.            
        }
        public static ArrayList alta()
        {

            string respuesta = "si";

            while(respuesta=="si")
            {       
                //Declaro los objetos y comienzo con la carga de datos
                Productos producto; 
                producto = new Productos();

                Console.WriteLine("Usted eligio: Productos y Promociones"+"\n");
                Console.Write("Ingrese Tipo: ");
                producto.tipo = Console.ReadLine();

                Console.Write("Ingrese Marca: ");
                producto.marca = Console.ReadLine();

                Console.Write("Ingrese Talle: ");
                producto.talle = int.Parse(Console.ReadLine());

                Console.Write("Ingrese Precio: ");
                producto.precio = int.Parse(Console.ReadLine());

                //Los agrego a la supuesta lista...

                AProducto.Add(producto);


                Console.Write("Cargado!"+"\n");
                Console.Write("Desea ingresar otro? ");
                respuesta=Console.ReadLine();       


            }

            // Devuelvo el producto para después utilizarlo.
            return AProducto;


        }
    }

}
    
asked by Mariano 17.05.2018 в 16:05
source

3 answers

2

The example that you put in definition var list= new List"object"; is incorrect. The definition of a generic list is List<T> , where T is the type of data that the list can contain.

Of course, for the case that you expose us, you could perfectly use var list = new List<object>() , but in that case you would lose all the advantages of using a generic list with defined types, such as the use of LINQ in it.

In your case, in the list you want to define the objects you want to enter are of type Productos , so the correct definition is var list = new List<Productos>(); . Adding objects of that type later is simply using the Add method in the list. If you try to put an object of another type, logically it will throw an exception. I put the complete code as it would be:

public class AltaDeProducto
{

    var AProducto = new List<Productos>();

    public List<Productos> alta()
    {
        string respuesta = "si";

        do
        {       
            //Declaro los objetos y comienzo con la carga de datos
            var producto = new Productos();

            Console.WriteLine("Usted eligio: Productos y Promociones"+"\n");
            Console.Write("Ingrese Tipo: ");
            producto.tipo = Console.ReadLine();

            Console.Write("Ingrese Marca: ");
            producto.marca = Console.ReadLine();

            Console.Write("Ingrese Talle: ");
            producto.talle = int.Parse(Console.ReadLine());

            Console.Write("Ingrese Precio: ");
            producto.precio = int.Parse(Console.ReadLine());

            //Los agrego a la supuesta lista...
            AProducto.Add(producto);

            Console.Write("Cargado!"+"\n");
            Console.Write("Desea ingresar otro? ");
            respuesta=Console.ReadLine(); 
        }while(respuesta=="si");

        // Devolvés la lista completa de todos los productos.
        return AProducto;
    }

    public void ImprimirLista()
    {
        foreach(var producto in AProducto)
        {
            Console.Write("Tipo: {0}. Marca: {1}. Talle: {2}. Precio: {3}.\n", producto.tipo, producto.marca, producto.talle, producto.precio);
        }
    }
}
    
answered by 17.05.2018 / 16:41
source
2

The correct definition for your first line would be:

var list = new List<object>();

In this way a generic list of type Object is created. But, it is advisable to create lists with specific data types, such as a generic string list (strings):

var list = new List<string>();

Now, to work with a character string there is another specialized class that is named StringCollection , which is basically the improvement of the string type [].

In the second code block you're talking about ArrayList , which is a vector of any type of data, that can grow or decrease dynamically.

using System;
using System.Collections;

namespace TP
{

    public class AltaDeProducto
    {

        var AProducto = new ArrayList();

        public ArrayList alta()
        {
            string respuesta = "si";

            do
            {       
                //Declaro los objetos y comienzo con la carga de datos
                var producto = new Productos();

                Console.WriteLine("Usted eligio: Productos y Promociones"+"\n");
                Console.Write("Ingrese Tipo: ");
                producto.tipo = Console.ReadLine();

                Console.Write("Ingrese Marca: ");
                producto.marca = Console.ReadLine();

                Console.Write("Ingrese Talle: ");
                producto.talle = int.Parse(Console.ReadLine());

                Console.Write("Ingrese Precio: ");
                producto.precio = int.Parse(Console.ReadLine());

                //Los agrego a la supuesta lista...
                AProducto.Add(producto);

                Console.Write("Cargado!"+"\n");
                Console.Write("Desea ingresar otro? ");
                respuesta=Console.ReadLine(); 
            }while(respuesta=="si");

            // Devolvés la lista completa de todos los productos.
            return AProducto;
        }

        public void ImprimirLista()
        {
            foreach(var producto in AProducto)
            {
                Console.Write("Tipo: {0}. Marca: {1}. Talle: {2}. Precio: {3}.\n", producto.tipo, producto.marca, producto.talle, producto.precio);
            }
        }
    }

}

I hope it's useful.

    
answered by 17.05.2018 в 16:24
1

If what you want is to declare a list of an object created by yourself, the syntax is as follows:

List<Producto> listaDeProductos = new List<Producto>();

If what you have is a product object for example and you want to add it to a list of products, you can do so by using the Add property of the lists.

Producto producto = new Producto()
{
nombreDeProducto = txtNombreProducto.Text,
etc.
};
listaDeProductos.Add(producto);

And with that you already have an example of the use of them!

I hope it serves you!

    
answered by 17.05.2018 в 16:41