Unassigned local variable C#

Asked

Viewed 634 times

0

Hello, I am at the beginning of my programming learning and I am having problems with this error "use of unassigned local variable" in the code below presents this error related to variable "salLiqui",I use it in the calculation of INSS normally, already in the Income Tax block it breaks, even though it has been used previously in the INSS block.

using System;

namespace Valendo1
{
    class Calculo_Trabalhista
    {
        static void Main(string[] args)
        {
            double vHora,qHora,salBrut,inss,irpf,salLiqui,baseCalcIrpf= 0;

            string msgIrpf = "0";

            do
            {
                Console.WriteLine("Para efetuar o calculo irei precisar que insira alguns dados.");
                Console.WriteLine("Primeiramente, qual o seu nome completo?");
                string nome = Console.ReadLine();
                Console.WriteLine("Qual a sua profissão?");
                string prof = Console.ReadLine();
                Console.WriteLine("Quantos dependentes você possui?");
                int qDep = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Quantas horas você trabalha por semana?");
                qHora = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Quanto recebe por hora?");
                vHora = Convert.ToDouble(Console.ReadLine());

                switch(vHora)
                {
                    case 0:
                        break;
                    default:
                        salBrut = (qHora * vHora)*4;

                        //Calcular INSS
                        if (salBrut <= 1317.07)
                        {
                            inss = salBrut * 0.08;
                            salLiqui = salBrut - inss;
                            baseCalcIrpf = salLiqui - qDep * 179.71;
                        }
                        else if (salBrut > 1317.07 && salBrut <=2195.12)
                        {
                            inss = salBrut * 0.09;
                            salLiqui = salBrut - inss;
                            baseCalcIrpf = salLiqui - qDep * 179.71;
                        }
                        else if (salBrut > 2195.12 && salBrut <= 4390.24)                       {
                            inss = salBrut * 0.12;
                            salLiqui = salBrut - inss;
                            baseCalcIrpf = salLiqui - qDep * 179.71;

                        }



                        //Calcular Imposto de Renda
                        if (salBrut <= 1787.77)
                        {
                            msgIrpf = "Sua renda não gera cobrança de IRPF!";
                        }
                        else if (salBrut> 1787.77 && salBrut <=2679.29)
                        {
                            irpf = baseCalcIrpf * 0.075;
                            salLiqui = salLiqui - irpf;

                        }
                        else if (salBrut >= 2679.30 && salBrut<= 3572.43)
                        {
                            irpf = baseCalcIrpf * 0.15;
                            salLiqui = salLiqui - irpf;
                        }
                        else if (salBrut >= 3572.44 && salBrut <=4463.81 )
                        {
                            irpf = baseCalcIrpf * 0.225;
                            salLiqui = salLiqui - irpf;
                        }
                        else
                        {
                            irpf = baseCalcIrpf * 0.275;
                            salLiqui = salLiqui - irpf;
                        }




                        break;



                }


            } while (vHora != 0);
        }
    }
}

Okay, I think it looks better to visualize, in the block above I use the same variable to calculate, but only in the next block I have this return.

  • 1

    The whole variable must be initialized. This means that on the double line vHora... you must put a value on all of them. You can do it the way you did and exchange the commas for = it will work too. Of whatever form, the correct one would be to leave the visible variables within the scope only that they are necessary

  • Sorry, still learning to use the forum, did not leave the top part of the code double vHora,qHora,salBrut,inss,irpf,salLiqui,baseCalcIrpf= 0; I initialized all it with 0, as I commented the variables were used in the first block normally, including the one of the supposed error, but when I opened another IF block it returns this message.

  • As stated, only the last variable was initialized as 0. Do: double vHora = qHora = salBrut = inss =irpf =salLiqui = baseCalcIrpf= 0;

  • It didn’t work that way suggested, but in fact I needed to declare all double vHora = 0,qHora=0,salBrut=0,inss=0,irpf=0,baseCalcIrpf= 0, salLiqui = 0; Thanks for the help.

  • There are several other errors in the code, it works, but it is not right. If you search here is a lot about the subject.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.