Luciano Nooijen
Luciano Nooijen

Reputation: 131

CS0649 error in C#

There's a bug in the code somewhere that I can't find, some variables simply don't work somehow.

The warning I get:

CS0649 Field 'Calculations.A' is never assigned to, and will always have its default value 0 ABC-Formule [path]\Calculations.cs

CS0649 Field 'Calculations.B' is never assigned to, and will always have its default value 0 ABC-Formule [path]\Calculations.cs

CS0649 Field 'Calculations.C' is never assigned to, and will always have its default value 0 ABC-Formule [path]\Calculations.cs

Code: Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ABC_Formule
{
    class Program
    {
        static void Main(string[] args)
        {
            bool keepGoing = true;
            while (keepGoing)
            {
                //Definieren strings gebruikt in Main
                string a;
                string b;
                string c;
                string keepGoingString;
                string discriminantString;

                double aDouble;
                double bDouble;
                double cDouble;

                double discriminantValue;
                double singleX;
                double doubleX1;
                double doubleX2;

                //Vraag de user om input
                Console.WriteLine("Welkom bij de ABC-Calculator. Gemaakt door Luciano Nooijen.");
                Console.WriteLine("Zorg voor een correcte invoer. De opbouw van een formule is ax^2 + bx + c.");
                Console.WriteLine("");
                Console.WriteLine("Vul de juiste variabelen in: (komma = ,)");
                Console.Write("a: "); a = Console.ReadLine();
                Console.Write("b: "); b = Console.ReadLine();
                Console.Write("b: "); c = Console.ReadLine();

                //Omzetten van string naar double 
                aDouble = Convert.ToDouble(a);
                bDouble = Convert.ToDouble(b);
                cDouble = Convert.ToDouble(c);
                               
                Console.WriteLine("a, b en c zijn: " + aDouble + ", " + bDouble + " en " + cDouble);
                Console.WriteLine("");

                //Invoeren getallen in berekenfunctie
                Calculations berekeningen = new Calculations(aDouble, bDouble, cDouble);

                //Discriminant berekenen
                discriminantValue = berekeningen.Discriminant();

                //Validatie discriminant en output
                discriminantString = berekeningen.ValidDiscriminant();
                if (discriminantString == "Gelijk") //Enkele output 
                {
                    singleX = berekeningen.OutputOnlyX();
                    Console.WriteLine("De discriminant is: " + discriminantValue);
                    Console.WriteLine("Het x-coördinaat is: " + singleX);
                }
                else if (discriminantString == "Groter") //Dubbele output
                {
                    doubleX1 = berekeningen.OutputX1();
                    doubleX2= berekeningen.OutputX2();
                    Console.WriteLine("De discriminant is: " + discriminantValue);
                    Console.WriteLine("Het x1-coördinaat is: " + doubleX1);
                    Console.WriteLine("Het x2-coördinaat is: " + doubleX2);
                }
                else if (discriminantString == "Kleiner") //Geen snijpunt
                {
                    Console.WriteLine("De discriminant is: " + discriminantValue + " en er is dus geen snijpunt met de x-as.");
                }
                else //Ongeldige invoer
                {
                    Console.WriteLine("Ongeldige invoer");
                }

                //Vragen of gebruiker door wil gaan
                Console.Write("Druk op enter om door te gaan, of druk op typ q om af te sluiten: "); keepGoingString = Console.ReadLine();
                if (keepGoingString.Equals("q"))
                {
                    keepGoing = false;
                }
                else
                {
                    Console.WriteLine("");
                    Console.WriteLine("-----------------------------------------------------------");
                    Console.WriteLine("");
                    Console.Clear();
                }              
            }

        }
    }
}

Calculations.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ABC_Formule
{
    class Calculations
    {
        protected double A;
        protected double B;
        protected double C;
        protected double DiscriminantValue;

        public Calculations (double a, double b, double c)
        {
            double A = a;
            double B = b;
            double C = c;
        }

        public double Discriminant ()
        {
            DiscriminantValue = B*B-4*A*C;
            return DiscriminantValue;
        }

        public string ValidDiscriminant()
        {
            if (DiscriminantValue < 0) //Geen uitkomst
            {
                return "Kleiner";
            }
            else if (DiscriminantValue == 0) //1 uitkomst
            {
                return "Gelijk";
            }
            else if (DiscriminantValue > 0) //Twee uitkomsten
            {
                return "Groter";
            }
            else //Bij fout
            {
                return "Error";
            }

        }

        public double OutputOnlyX ()
        {
            return (-B + Math.Sqrt(DiscriminantValue) ) / (2 * A);
        }

        public double OutputX1 ()
        {
            return (-B - Math.Sqrt(DiscriminantValue)) / (2 * A);
        }

        public double OutputX2 ()
        {
            return (-B + Math.Sqrt(DiscriminantValue)) / (2 * A);
        }
    }
}

Upvotes: 4

Views: 674

Answers (1)

BartoszKP
BartoszKP

Reputation: 35901

In Calculations constructor you have:

double A = a;

you want:

A = a; // or this.A = a;

instead(this of course concerns also B and C).

In your code, you create a local (in the scope of the constructor) variable A that has nothing to do with the class's A field (beside having, confusingly, the same name). What happens in such a situation is called shadowing, effectively causing every A in this function's code to mean the local variable, not the field in the parent scope.

Upvotes: 4

Related Questions