C# - 双重转换的文本抛出System.TypeInitializationException

时间:2015-12-12 09:51:39

标签: c# wpf xaml exception

所以我有2个类,一个名为 DataTypes.cs ,另一个名为 Doelen.xaml.cs

代码DataTypes.cs:

//Doelen
    public static double Omzet = 0;
    public static double Marge = 0;
    public static double NieuweKlanten = 0;

    //capaciteit
    public static double cUurPWeek = 0;
    public static double cUurPWeekSF = 0;
    public static double cPercTot = 0;

    //saleskit
    public static int skProspects = 0;
    public static int skHotProspects = 0;
    public static int afsMak = 0;
    public static int afs = 0;
    public static int offMak = 0;
    public static int gescOff = 0;
    public static int newKlant = 0;

    //conversie
            public static double convPHP = (Convert.ToDouble(skHotProspects / skProspects)) * 100;
    public static double convHPAM = (Convert.ToDouble(afsMak / skHotProspects)) * 100;
    public static double convAMA = (Convert.ToDouble(afs / afsMak)) * 100;
    public static double convAOM = (Convert.ToDouble(offMak / afs)) * 100;
    public static double convOMGO = (Convert.ToDouble(gescOff / offMak)) * 100;
    public static double convGONK = (Convert.ToDouble(newKlant / gescOff)) * 100;

代码Doelen.xaml.cs:

private void btnNext_Click(object sender, RoutedEventArgs e)
{
    DataTypes.Omzet = Convert.ToDouble(txxOmzetMaand.Text);
    DataTypes.Marge = Convert.ToDouble(txxMargeMaand.Text);
    DataTypes.NieuweKlanten = Convert.ToDouble(txxNieuwKlantMaand.Text);
}

代码Doelen.xaml

<StackPanel x:Name="datFieldMnd" Orientation="Vertical" Margin="0,15,10,0">
<TextBox x:Name="txxOmzetMaand" TextWrapping="Wrap" Width="40px" Height="20px" Margin="0,7,0,0"/>
<TextBox x:Name="txxMargeMaand" TextWrapping="Wrap" Width="40px" Height="20px" Margin="0,7,0,0"/>
<TextBox x:Name="txxNieuwKlantMaand" TextWrapping="Wrap" Width="40px" Height="20px" Margin="0,7,0,0"/>
</StackPanel>

当我点击btnNext时会抛出此异常:

  

System.TypeInitializationException未处理   消息:未处理的类型&#39; System.TypeInitializationException&#39;发生在SalesKicker2.exe中   附加信息:&#39; SalesKicker2.DataTypes&#39;的类型初始值设定项。抛出异常。

我在这里做错了什么?

我还看了一下这个问题:WPF Type initialization Exception in C#。但这个问题的答案根本没有帮助。一个人甚至说他已经创建了一个新项目,这就是我不会做的事情。我还尝试导入以下软件包:

using System; 
using System.Collections.Generic; 
using System.Configuration; 
using System.Data; 
using System.Linq; 
using System.Windows;

这并没有改变一件事。

那么,再一次,有人可以帮助我吗?

提前致谢!

1 个答案:

答案 0 :(得分:2)

所有这些初始化程序:

public static double convPHP = (skHotProspects / skProspects) * 100;
public static double convHPAM = (afsMak / skHotProspects) * 100;
public static double convAMA = (afs / afsMak) * 100;
public static double convAOM = (offMak / afs) * 100;
public static double convOMGO = (gescOff / offMak) * 100;
public static double convGONK = (newKlant / gescOff) * 100;

将抛出DivideByZeroException

我认为你的意思是使用属性,而不是字段。例如

public static double ConvPHP { get { return (skHotProspects / skProspects) * 100; } }

或在C#6

public static double ConvPHP => (skHotProspects / skProspects) * 100;

如果在skProspects为0时尝试获取值,这些仍会抛出异常,但可以正确初始化类型。

您也应该将其他字段更改为属性并添加验证以确保您永远不会尝试除以0.或者在除以之前检查值并返回一些常量而不是除以0

public static double ConvPHP 
{ 
    get 
    { 
        if (skProspects == 0) return 0;
        return (skHotProspects / skProspects) * 100; 
    } 
}