C#-文本到双精度的转换引发System.TypeInitializationException
本文关键字:System TypeInitializationException 转换 文本 双精度 C#- | 更新日期: 2023-09-27 18:26:28
所以我有两个类,一个叫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消息:SalesKicker2.exe中发生类型为"System.TypeInitializationException"的未处理异常其他信息:"SalesKicker2.DataTypes"的类型初始值设定项引发异常。
我在这里做错了什么?
我还研究了这个问题:C#中的WPF类型初始化异常。但是这个问题的答案一点帮助都没有。一个人甚至说他必须创建一个新项目,这是我不会做的。我还尝试导入以下包:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
这并没有改变任何事情。
那么,再一次,有人能帮我吗?
提前感谢!
所有这些初始化程序:
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;
将抛出CCD_ 1。
我认为你的意思是使用属性,而不是字段。例如
public static double ConvPHP { get { return (skHotProspects / skProspects) * 100; } }
或在C#中
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;
}
}