Visual Studio声明变量未初始化,但它已在构造函数中

时间:2015-03-29 14:36:59

标签: c# f#

我正在搞乱WPF,创建一个类似应用程序的小型计算器。只是想尝试F#我写了一个类来处理所有的数学函数,并在我的WPF应用程序中引用它。在我的C#代码中 -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MathLib;

namespace MathToolApplication
{
    public partial class MainWindow : Window
    {
        private MathLib.Math tool;

        public MainWindow()
        {
            InitializeComponent();
            this.tool = new MathLib.Math();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Double a = Double.Parse(this.a.Text);
            Double b = Double.Parse(this.b.Text);

            try
            {
                switch (this.operation.Text)
                {
                    case "+": this.result.Text = this.tool.add(a, b).ToString(); break;
                    case "-": this.result.Text = this.tool.subtract(a, b).ToString(); break;
                    case "*": this.result.Text = this.tool.multiply(a, b).ToString(); break;
                    case "÷": this.result.Text = this.tool.divide(a, b).ToString(); break;
                    case "²": this.result.Text = this.tool.square(a).ToString(); break;
                    case "³": this.result.Text = this.tool.cube(a).ToString(); break;
                    case "To the power of": this.tool.toThePowerOf(a, b).ToString(); break;
                    case "++": this.result.Text = this.tool.plusplus(a).ToString(); break;
                    case "--": this.result.Text = this.tool.minusminus(a).ToString(); break;
                    default: this.result.Text = "Error."; break;
                }
            }
            catch (FormatException)
            {
                this.result.Text = "Error.";
            }
        }

        private void operation_DropDownClosed(object sender, EventArgs e)
        {
            string op = this.operation.Text;

            switch (op)
            {
                case "²": this.b.IsEnabled = false; break;
                case "³": this.b.IsEnabled = false; break;
                case "++": this.b.IsEnabled = false; break;
                case "--": this.b.IsEnabled = false; break;
                default: this.b.IsEnabled = true; break;
            }
        }
    }
}

Visual Studio在private MathLib.Math tool;处标记警告声称它未初始化,并且将为null。但是如果你看一下我的构造函数,我会初始化它。除此之外,我已尝试声明ti并在private MathLib.Math tool = new MathLib.Math();之类的语句中初始化它。我不知道问题是什么,但我觉得它可能与我的F#有关,所以它发布在这里:

namespace MathLib

type Math() = 
    member this.add(a:float, b:float) =
        a+b

    member this.subtract(a:float, b:float) =
        a-b

    member this.multiply(a:float, b:float) =
        a*b

    member this.divide(a:float, b:float) =
        a/b

    member this.square(a:float) =
        a*a

    member this.cube(a:float) =
        a*a*a

    member this.toThePowerOf(a:float, b:float) =
        a**b

    member this.plusplus(a:float) =
        let mutable count = a
        count <- count + 1.0
        count

    member this.minusminus(a:float) =
        let mutable count = a
        count <- count + 1.0
        count

issue

1 个答案:

答案 0 :(得分:1)

像这样修改:

private MathLib.Math tool = null;

Visual Studio中的警告不够聪明,无法在构造函数中找到初始化。