Forum guidelines
AuthorQuestion
variable is assigned but never used
Posted on: 09 Apr 2013
Hi..what's wrong with this code? I declared the variable pret, and then used it, but i'm informed that is assigned but never used.

     when   form load{     
        
                double pret;// it's assigned but never used
            if (cnp.StartsWith("1") || cnp.StartsWith("2"))
            {
                string an = "19" + cnp.Substring(1, 2);
                string luna = cnp.Substring(3, 2);
                string zi = cnp.Substring(5, 2);
                string data_nastere = luna + "/" + zi + "/" + an;

                DateTime data = Convert.ToDateTime(data_nastere);
                TimeSpan zile = DateTime.Today.Subtract(data);
                varsta = Convert.ToInt32(zile) / 356;
                if (varsta <= 23)
                {
                    if (double.Parse(tb2.Text) <= 1200)
                        pret = 340;
                    else if (double.Parse(tb2.Text) >= 1201 && double.Parse(tb2.Text) <= 1600)
                        pret = 486;
                    else if (double.Parse(tb2.Text) >= 1601 && double.Parse(tb2.Text) <= 2000)
                        pret = 535;
                    else if (double.Parse(tb2.Text) >= 2001 && double.Parse(tb2.Text) <= 2400)
                        pret = 583;
                    else if (double.Parse(tb2.Text) >= 2401 && double.Parse(tb2.Text) <= 3000)
                        pret = 729;
                    else if (double.Parse(tb2.Text) <= 3001)
                        pret = 1458;
                }
}

AuthorReply
Re: variable is assigned but never used
Posted on: 09 Apr 2013  
Since the variable pret is assigned in a conditional statement the compiler would not know and the value is assigned only at runtime, to avoid this you can assign  a default value to your variable:

double pret=0;

Employers - Post Free Jobs

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.