1
I need to make a code that calculates the annual salary (float
) from the months worked (int
) and monthly salary (float
), but it has to be using the constructor method.
Here’s a code I made but it’s returning 0 to the value of the annual salary:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SalarioAnual
{
class Program
{
static void Main(string[] args)
{
Calculo c = new Calculo();
Console.Write("Informe o numero de meses trabalhados: ");
c.Meses = int.Parse(Console.ReadLine());
Console.Write("Informe o salário mensal: ");
c.Salario = float.Parse(Console.ReadLine());
Console.WriteLine("O salário anual é: " + c.SalarioAnual);
}
}
}
And here the class Cálculo
, with attributes in the constructor method:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SalarioAnual
{
class Calculo
{
int _Meses;
float _Salario;
float _SalarioAnual;
public int Meses
{
set { _Meses = value; }
get { return _Meses; }
}
public float Salario
{
set { _Salario = value; }
get { return _Salario; }
}
public float SalarioAnual
{
set { _SalarioAnual = _Meses * _Salario; }
get { return _SalarioAnual; }
}
}
}
A new code using a constructor:
using static System.Console;
namespace SalarioAnual {
public class Program {
public static void Main() {
int Meses;
float Salario;
Write("Informe o numero de meses trabalhados: ");
Meses = int.Parse(ReadLine());
Write("Informe o salário mensal: ");
Salario = float.Parse(ReadLine());
var c = new Calculo(Meses, Salario);
WriteLine("O salário anual é: " + c.SalarioAnual);
}
public class Calculo
{
float _SalarioAnual;
public Calculo(int meses, float salario)
{
_SalarioAnual = meses * salario;
}
public float SalarioAnual
{ get { return _SalarioAnual; } }
}
}
}
Hello @Full Welcome to Stackoverflow, please edit your question to make it clearer as your problem cannot be clearly understood, I suggest you visit https://answall.com/help/how-to-ask
– Lucas Duete
William, can you read ( https://www.dotnetperls.com/constructor ) and rephrase your question so that we can help you.
– SylvioT
Just waiting for approval to send you the code friend, but it’s simple.
– Victor Laio