-1

I'm trying to understand how to properly pass a parameter and more importantly, return one or several values.

In the main method I have:

    public IActionResult FinVD()
    {

        var user = from d in _context.Roles
                   join userRole in _context.UserRoles on d.Id equals userRole.RoleId
                   join usuario in _context.Users on userRole.UserId equals usuario.Id
                   where usuario.UserName == User.Identity.Name
                   select d;

        var cargo = user.FirstOrDefault();
        var cargodesc = cargo.Name;

        ListaTienda(cargodesc);
        //...More code
        FinanzaDIndexData ventadiaria = new FinanzaDIndexData()
        {
            MedioPagos = medio_pago,
            hechosf_periodo = fecha,
            //Here lies the problem. Is not initialized in the context if the code stays like this.
            HechosFinanzaL = ListaHechosFinanza,
        };
        return View(ventadiaria);
        }

Passing this parameter to ListaTienda will do the following:

  • Generate a ViewBag
  • Generate a List

    private void ListaTienda(string cargodesc)
    {
        if (cargodesc == "Jefe")
        {
            List<Tienda> ListaTienda = new List<Tienda>();
            ListaTienda = _context.Stores.Where(j => j.Districts.Provincias.provincia_jefe == User.Identity.Name && j.tienda_vigencia_reg == 1).OrderBy(j => j.tienda_nombre).ToList();
            ListaTienda.Insert(0, new Tienda { tienda_id = 0, tienda_nombre = "-- Seleccione Tienda --" });
            ViewBag.ListaTienda = ListaTienda;
    
            List<HechosFinanza> ListaHechosFinanza = new List<HechosFinanza>();
            ListaHechosFinanza = _context.HechosFinanza.Include(f => f.Devices).ThenInclude(f => f.Machines).ThenInclude(f => f.Stores).ThenInclude(f => f.Districts).ThenInclude(f => f.Provincias)
                .Where(f => f.finanzas_id == 1 &&
                f.Devices.Machines.Stores.Districts.Provincias.provincia_jefe == User.Identity.Name)
                .OrderBy(f => f.Devices.Machines.Stores.tienda_nombre).ToList();
               //...more code
        }
    }
    

The problem:

When I go back to the main method, I have a List waiting to be populated with the result of ListaHechosFinanza but I get the message:

The name 'ListaHechosFinanza' does not exist in the actual context

Do I have to declare an empty List<HechosFinanza> in the main method, pass it as a parameter and let it be modified using ref? or without it since is a List? Like this?:

    public IActionResult FinVD()
    {

        var user = from d in _context.Roles
                   join userRole in _context.UserRoles on d.Id equals userRole.RoleId
                   join usuario in _context.Users on userRole.UserId equals usuario.Id
                   where usuario.UserName == User.Identity.Name
                   select d;

        var cargo = user.FirstOrDefault();
        var cargodesc = cargo.Name;

        List<HechosFinanza> ListaHechosFinanza = new List<HechosFinanza>();

        ListaTienda(cargodesc, ListaHechosFinanza);
        //... more code
        FinanzaDIndexData ventadiaria = new FinanzaDIndexData()
        {
            MedioPagos = medio_pago,
            hechosf_periodo = fecha,
            HechosFinanzaL = ListaHechosFinanza,
        };
        return View(ventadiaria);
    }

Part 2:

    private void ListaTienda(string cargodesc, List<HechosFinanza> ListaHechosFinanza)
    {
        if (cargodesc == "Jefe")
        {
            List<Tienda> ListaTienda = new List<Tienda>();
            ListaTienda = _context.Stores.Where(j => j.Districts.Provincias.provincia_jefe == User.Identity.Name && j.tienda_vigencia_reg == 1).OrderBy(j => j.tienda_nombre).ToList();
            ListaTienda.Insert(0, new Tienda { tienda_id = 0, tienda_nombre = "-- Seleccione Tienda --" });
            ViewBag.ListaTienda = ListaTienda;

            //List<HechosFinanza> ListaHechosFinanza = new List<HechosFinanza>();
            ListaHechosFinanza = _context.HechosFinanza.Include(f => f.Devices).ThenInclude(f => f.Machines).ThenInclude(f => f.Stores).ThenInclude(f => f.Districts).ThenInclude(f => f.Provincias)
                .Where(f => f.finanzas_id == 1 &&
                f.Devices.Machines.Stores.Districts.Provincias.provincia_jefe == User.Identity.Name)
                .OrderBy(f => f.Devices.Machines.Stores.tienda_nombre).ToList();
        }

1 Answers1

0

ListaTienda() method needs to return a list of HechosFinanza.

So change it to

private IList<HechosFinanza> ListaTienda(string cargodesc, List<HechosFinanza> ListaHechosFinanza)

Also, since this method returns a value now, you need to assign its value to ListaHechosFinanza in FinVD() which will look like:

IList<HechosFinanza> ListaHechosFinanza = ListaTienda(cargodesc, ListaHechosFinanza);

This error message means that the variable ListaHechosFinanza is not 'visible' to the ListaTienda method as this is a local variable.

The name 'ListaHechosFinanza' does not exist in the actual context

Head over to MSDN to understand the scoping of the variables which is at the crux of data sharing and access.

Also, always code to interface, not implementation. More info here - What does it mean to "program to an interface"?

h-rai
  • 3,636
  • 6
  • 52
  • 76