Visual Studio claims 变量未初始化,但已在构造函数中
Visual Studio claims variable is not initialized, but it has been in the constructor
我正在摆弄 WPF,创建一个类似于应用程序的小型计算器。只是想尝试 F#,我写了一个 class 来处理所有数学函数,并在我的 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;
标记警告,声称它未初始化,将为空。但是如果你看一下我的构造函数,我会初始化它。除此之外,我还尝试在 private MathLib.Math tool = new MathLib.Math();
这样的语句中声明 ti 并对其进行初始化。我不知道问题是什么,但我觉得它可能与我的 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
修改如下:
private MathLib.Math tool = null;
Visual studio中的警告不够聪明,无法在构造函数中找到初始化。
我正在摆弄 WPF,创建一个类似于应用程序的小型计算器。只是想尝试 F#,我写了一个 class 来处理所有数学函数,并在我的 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;
标记警告,声称它未初始化,将为空。但是如果你看一下我的构造函数,我会初始化它。除此之外,我还尝试在 private MathLib.Math tool = new MathLib.Math();
这样的语句中声明 ti 并对其进行初始化。我不知道问题是什么,但我觉得它可能与我的 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
修改如下:
private MathLib.Math tool = null;
Visual studio中的警告不够聪明,无法在构造函数中找到初始化。