0

so... it's a pretty dumb question but i was searching and didn't found a didatic answer...

I created this Class on my Helpers folder, to use it on the controller, to make some clean code...

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

namespace CarroBemGuardado.Helpers
{
    public class CalcularValorPagar
    {
        static decimal CalcularPagamento (string dur, decimal ph, decimal hadd)
        {
            decimal valorpagar = 0;
            decimal horas = Convert.ToDecimal(dur.Substring(0, 2));
            decimal minutos = Convert.ToDecimal(dur.Substring(3, 2));

            if ((horas == 0) && (minutos <= 30))
            {
                valorpagar = ph / 2;
            }
            else if ((horas > 0) && (minutos > 10))
            {
                valorpagar = ph + (hadd * horas);
            }
            else if ((horas > 0) && (minutos < 10))
            {
                valorpagar = ph + (hadd * (horas - 1));
            }
            else
            {
                valorpagar = ph;
            }

            return valorpagar;
        }
    }
}

But i'm trying to call the method CalcularPagamento on my controller (and pass the 3 parameters to it) but i can't. I can only call the Class. I tried to instantiate the Class to call the method but also didn't worked.

How can i do that ?

Lucas Barreto
  • 140
  • 3
  • 12

2 Answers2

1

your code should be changed like this so you can access the helper method from other class

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

namespace CarroBemGuardado.Helpers
{
    public class CalcularValorPagar
    {
       public static decimal CalcularPagamento (string dur, decimal ph, decimal hadd)
        {
            decimal valorpagar = 0;
            decimal horas = Convert.ToDecimal(dur.Substring(0, 2));
            decimal minutos = Convert.ToDecimal(dur.Substring(3, 2));

            if ((horas == 0) && (minutos <= 30))
            {
                valorpagar = ph / 2;
            }
            else if ((horas > 0) && (minutos > 10))
            {
                valorpagar = ph + (hadd * horas);
            }
            else if ((horas > 0) && (minutos < 10))
            {
                valorpagar = ph + (hadd * (horas - 1));
            }
            else
            {
                valorpagar = ph;
            }

            return valorpagar;
        }
    }
}
Rohit Poudel
  • 1,793
  • 2
  • 20
  • 24
1

You need to provide public Access-Modifier for your static method

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

namespace CarroBemGuardado.Helpers
{
    public class CalcularValorPagar
    {
        public static decimal CalcularPagamento (string dur, decimal ph, decimal hadd)
        {
            decimal valorpagar = 0;
            decimal horas = Convert.ToDecimal(dur.Substring(0, 2));
            decimal minutos = Convert.ToDecimal(dur.Substring(3, 2));

            if ((horas == 0) && (minutos <= 30))
            {
                valorpagar = ph / 2;
            }
            else if ((horas > 0) && (minutos > 10))
            {
                valorpagar = ph + (hadd * horas);
            }
            else if ((horas > 0) && (minutos < 10))
            {
                valorpagar = ph + (hadd * (horas - 1));
            }
            else
            {
                valorpagar = ph;
            }

            return valorpagar;
        }
    }
}

And in your Controller, you could access it as follow:

public ActionResult Index(){
    string sDur = "someValue";
    decimal dPH = 2;
    decimal dHadd = 3;
    decimal valorpagar = CalcularValorPagar.CalcularPagamento(sDur, dPH, dHadd );

   return View();
}

You don't need to instantiate your helper class CalcularValorPagar to access your any static methods within CalcularValorPagar class.

This is the real beauty (i like the most) of static methods that you could call them without creating instance of class in which these static methods reside.

Related SO Threads(This & This ) for Static methods.

mmushtaq
  • 3,430
  • 7
  • 30
  • 47
  • It's not necessary to initialize the class with CalcularValorPagar abc = new CalcularValorPagar(); ? – Lucas Barreto Jul 10 '17 at 05:29
  • yes it is not necessary to instantiate your class. Please have a look at [this SO Question](https://stackoverflow.com/questions/4124102/whats-a-static-method-in-c) – mmushtaq Jul 10 '17 at 05:33