How to pass an ArrayList from one class to a method within another Class?

2

I need to use a ArrayList object object that I have in a class, in one method that I have in another. The files within the project are separate and I have inside one, a function that works on the attributes of the objects within the ArrayList and I need to pass them to the method.

This I have in a file within a project called Alta de Producto :

public class AltaDeProducto
{
    public static void alta()
    {
        ArrayList AProducto = new ArrayList();
        string respuesta = "si";
        ... 
    }
}

In the file called Promociones , where I have the function, I do not know how to pass the ArrayList :

public class Promociones
{   
    public static void descuento( ACA NO SE COMO PASARLE EL ARRAY)
    {
        foreach(Productos item in desc)
        {
            Console.WriteLine("Tipo: " + item.tipo);
            Console.WriteLine("Marca: " + item.marca);
            Console.WriteLine("Talle: " + item.talle);
            Console.WriteLine("Precio: " + item.precio);
            // Esto es para imprimir lo que el objeto tiene.

        }
    }

From this file I call the function Promociones :

public static void mascara()
{
    int opcion = 1;

    while(opcion!=5)
    {       
        Console.Write("Opcion: ");
        opcion = int.Parse(Console.ReadLine());

        switch(opcion)
        {
            case 1:
                Console.Clear();
                AltaDeProducto.alta();
                break;
            case 2:
                Console.Crear();
                Promociones.descuento();
                break;
        }
    }
}
    
asked by Mariano 16.05.2018 в 17:02
source

1 answer

3

To solve your problem you must modify your alta method so that it returns the ArrayList to the main method. Something like this:

public static ArrayList alta()
{
    ArrayList AProducto = new ArrayList();
    string respuesta = "si";
    ... 

    return Aproducto;
}

That way, in your main method, you receive it in another variable, so you can use it to call your second method. Something like this:

public static void mascara()
{
    int opcion = 1;

    ArrayList arrAlta;

    while(opcion!=5)
    {       

    Console.Write("Opcion: ");
    opcion = int.Parse(Console.ReadLine());

    switch(opcion)
    {
        case 1:
            Console.Clear();
            arrAlta = AltaDeProducto.alta();
            break;
        case 2:
            Console.Crear();
            if (arrAlta !=null)
            {
                Promociones.descuento(arrAlta);
            }
            break;
        }
    }
}

Finally, you must add the parameter to your Descuento method:

public static void descuento(ArrayList arr)
{
     ....
}

A couple of extra considerations. ArrayList is an obsolete class. Your generic substitute List<T> should be used. On the other hand, this works because you use your own language class. If you use a class of your own, it should be public to be available in all classes by using .

    
answered by 16.05.2018 / 17:18
source