C#中的CS0649错误 [英] CS0649 error in C#

查看:72
本文介绍了C#中的CS0649错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码中有一个我找不到的错误,有些变量根本无法工作.

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

我得到的警告:

CS0649字段"Calculations.A"从未分配,并且其默认值始终为0 ABC-Formule [path] \ Calculations.cs

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

CS0649字段"Calculations.B"从未分配,并且其默认值始终为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字段"Calculations.C"从未分配,并且其默认值始终为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

代码:Program.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

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);
        }
    }
}

推荐答案

Calculations 构造函数中,您具有:

In Calculations constructor you have:

double A = a;

您想要的:

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

代替(当然这也涉及 B C ).

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

在代码中,您将创建一个本地变量(在构造函数的范围内) A ,该变量与该类的 A 字段无关(除了令人困惑之外,,同名).在这种情况下发生的事情称为阴影,有效地导致了每个 A 在此函数的代码中表示局部变量,而不是父作用域中的字段.

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.

这篇关于C#中的CS0649错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆