You can return the value of tot
from within the function CadastroDespesas
exchanging void
by the type of varivell you want to return, in your case is a float
, its function would have this header:
public float CadastroDespesa(){
}
And at the end of it you return tot
:
return tot;
Now you need to change the method MostrarDespesas
asking for a float to be displayed:
public void MostrarDespesas(float valorTotalDespesas)
{
}
The correct thing is first you receive the value of CadastroDespesa
and then call MostrarDespesas
passing the amount received:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
Ex4 DespesaManager = new Ex4();
float tot = DespesaManager.CadastroDespesa();
DespesaManager.MostrarDespesas(tot);
}
}
}
This is how you would use the changes. Your code itself looks like this:
class Ex4
{
List<String> descricaodespesas = new List<string>();
List<float> valordespesas = new List<float>();
List<String> descricaoreceitas = new List<string>();
List<float> valorreceitas = new List<float>();
public float CadastroDespesas()
{
string descricao;
float valor;
float soma = 0,tot;
Console.Write("Informe o valor: ");
valor = Convert.ToSingle(Console.ReadLine());
valordespesas.Add(valor);
Console.WriteLine("Informe uma descrição:");
descricao = (Console.ReadLine());
descricaodespesas.Add(descricao);
foreach (float som in valordespesas)
{
soma += valor;
}
tot = soma;
return tot;
}
public void MostrarDespesas(float valorTotalDespesas)
{
Console.WriteLine("------Despesas Informadas------");
Console.WriteLine();
for(int i = 0; i < descricaodespesas.Count; i++)
{
string desc = descricaodespesas.ElementAt(i);
float valor = valordespesas.ElementAt(i);
Console.Write("Descrição: "); Console.WriteLine(desc);
Console.Write("Valor: "); Console.WriteLine(valor);
}
Console.WriteLine("Valor total: ");
Console.WriteLine(valorTotalDespesas);
Console.ReadLine();
}
This is your way of doing it, but I think it would be more interesting to use the list itself valordespesas
to add, you can use LINQ like this:
valordespesas.Sum();
So you wouldn’t need to return anything in your duties, MostrarDespesas
gets like this:
public void MostrarDespesas()
{
Console.WriteLine("------Despesas Informadas------");
Console.WriteLine();
for(int i = 0; i < descricaodespesas.Count; i++)
{
string desc = descricaodespesas.ElementAt(i);
float valor = valordespesas.ElementAt(i);
Console.Write("Descrição: "); Console.WriteLine(desc);
Console.Write("Valor: "); Console.WriteLine(valor);
}
Console.WriteLine("Valor total: ");
Console.WriteLine(valordespesas.Sum());
Console.ReadLine();
}
put it outside the method in the general scope of the class then all methods will have access.
– novic
declares out, same as what made for the descricaodespesas
– aa_sp
solved your problem?
– Marco Souza
Yes. Thank you all for your help.
– Guilherme Schubert Medeiros
Behold: How and why to accept an answer?
– Math