Reputation:
So I have 2 classes, one called DataTypes.cs and one called Doelen.xaml.cs
code 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;
code 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);
}
code 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>
When I click btnNext it throws this exception:
System.TypeInitializationException was unhandled Message: An unhandled exception of type 'System.TypeInitializationException' occurred in SalesKicker2.exe Additional information: The type initializer for 'SalesKicker2.DataTypes' threw an exception.
What am I doing wrong here?
I also took a look at this question: WPF Type initialization Exception in C#. But the answers to this question didn't help at all. One guy even said that he had t0 create a new project and that's what I`m not going to do. And I've also tried to import the following packages:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
This didn't change a thing.
So, once again, can someone help me out?
Thanks in advance!
Upvotes: 1
Views: 136
Reputation: 14896
All of these initializers:
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;
will throw a DivideByZeroException
.
I think you meant to use properties, not fields. For example
public static double ConvPHP { get { return (skHotProspects / skProspects) * 100; } }
or in C# 6
public static double ConvPHP => (skHotProspects / skProspects) * 100;
These will still throw an exception if you try to get the value when skProspects
is 0, but the type can be initialized correctly.
You should change the other fields to properties too and add validation to make sure you never try to divide by 0. Or check the value before dividing and return some constant instead of dividing by 0
public static double ConvPHP
{
get
{
if (skProspects == 0) return 0;
return (skHotProspects / skProspects) * 100;
}
}
Upvotes: 2