Unable to access a private variable from a view

1

Hi, I have to make a class employee scheduler that inherits from the base class employed and then I have to calculate the salary.

HomeController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Empresa2.Models;

namespace Empresa2.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        //public ActionResult Index()
        //{
        //    return View();
        //}

        public ActionResult Programador()
        {
            EmpleadoProgramador emp1prog = new EmpleadoProgramador();
            emp1prog.Nombre = "Ezequiel";
            emp1prog.Apellido = "Perez";
            float sueldo1 = emp1prog.ObtenerSueldo();            
            sueldo1 = 15000;
            //CalcularSueldo("Adrian", "suarez",20);
           // emp1prog.ObtenerSueldo();
            return View(emp1prog);
        }


    }
}

Empleado.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Empresa2.Models
{
    public abstract class Empleado
    {
        public string Nombre { set; get; }
        public string Apellido { set; get; }
        public Empleado() { }

        public Empleado(string Nombre, string Apellido)
        {
            Nombre = this.Nombre;
            Apellido = this.Apellido;
        }

        /*  public abstract float CalcularSueldo();
          } */

    }
}

EmployeeProgrammer.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Empresa2.Models
{
    public class EmpleadoProgramador : Empleado
    {
        private float SueldoFijo { set; get; }


        public float ObtenerSueldo()
        {
            return SueldoFijo;
        }

        private EmpleadoProgramador(string Nombre, string Apellido, int horas, float valorHora, float incentivo)
            : base(Nombre, Apellido)
        {

            valorHora = 50;
            incentivo = 5000;
            CalcularSueldo(Nombre, Apellido, horas, valorHora, incentivo);
        }



        public EmpleadoProgramador()
        {
        }

        private float CalcularSueldo(string Nombre, string Apellido, int horas, float valorHora, float incentivo)
        {

            float Sueldo = (valorHora * horas) + incentivo;
            return Sueldo;
        }


    }
}

Programmer.cshtml

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Programador</title>
</head>
<body>
    <div>
        @Model.Nombre
        <br />
        @Model.Apellido
        <br />
        @Model.ObtenerSueldo()
    </div>
</body>
</html>

However, when I want to show the salary, it shows it as zero, what is happening?

    
asked by Andrés Oporto 02.05.2017 в 20:00
source

3 answers

0

Shows you 0 because you never initialize the Fixed Salary property.

On the other hand, if GetLoan () only returns the value of your private property SuledoFijo, I suggest you better think of it as a property:

    private float sueldoFijo;
    public float SueldoFijo
    {
        get { return sueldoFijo; }
        set { sueldoFijo = value; }
    }
    
answered by 02.05.2017 / 20:30
source
0

The truth has quite a few coding errors. Try changing these two files as follows:

HomeController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Empresa2.Models;

namespace Empresa2.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        //public ActionResult Index()
        //{
        //    return View();
        //}

        public ActionResult Programador()
        {
            EmpleadoProgramador emp1prog = new EmpleadoProgramador("Ezequiel", "Perez", 10, 1000, 500);            
            return View(emp1prog);
        }


    }
}

EmployeeProgrammer.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Empresa2.Models
{
    public class EmpleadoProgramador : Empleado
    {
        private int _horas = 0;
        private float _valorHora = 0;
        private float _incentivo = 0;

        public float ObtenerSueldo()
        {
            return (_valorHora * _horas) + _incentivo;;
        }

        private EmpleadoProgramador(string Nombre, string Apellido, int horas, float valorHora, float incentivo)
            : base(Nombre, Apellido)
        {
            _horas = horas;
            _valorHora = valorHora;
            _incentivo = incentivo;            
        }        
    }
}
    
answered by 02.05.2017 в 20:30
0

The problem is because nowhere do you assign a value to SueldoFijo .

You can fix it by assigning a value to SueldoFijo when you calculate the employee's salary:

private float CalcularSueldo(string Nombre, string Apellido, int horas, float valorHora, float incentivo)
{
    SueldoFijo = (valorHora * horas) + incentivo;
    return SueldoFijo;
}
    
answered by 02.05.2017 в 20:32