i have a class:
class GetColumnsNames
{
public static int Occurrence_Date = Convert.ToInt16(ConfigurationSettings.AppSettings["Occurrence_Date"].ToString());
public static int Preanalytical_Before_Testing = Convert.ToInt16(ConfigurationSettings.AppSettings["1_0_Preanalytical_Before_Testing"].ToString());
public static int Cup_Type = Convert.ToInt16(ConfigurationSettings.AppSettings["Cup_Type"].ToString());
public static int Analytical_Testing_Phase = Convert.ToInt16(ConfigurationSettings.AppSettings["Analytical_Testing_Phase"].ToString());
public static int Area = Convert.ToInt16(ConfigurationSettings.AppSettings["Area"].ToString());
public static int Postanalytical_After_Testing = Convert.ToInt16(ConfigurationSettings.AppSettings["Postanalytical_After_Testing"].ToString());
public static int Other = Convert.ToInt16(ConfigurationSettings.AppSettings["Other"].ToString());
public static int Practice_Code = Convert.ToInt16(ConfigurationSettings.AppSettings["Practice_Code"].ToString());
public static int Comments = Convert.ToInt16(ConfigurationSettings.AppSettings["Comments"].ToString());
}
i need to be able to reference the values in this class like this without initializing the class:
int var1 = GetColumnsNames.Area + 1
i am getting an error like this:The type initializer for 'BulkUploadToLOMDatabase.GetColumnsNames' threw an exception.
what am i doing wrong>?
Your class has static properties that must initialize when the type is loaded at all.
One of your static properties is throwing an exception. Since you aren’t checking
anything, if one of the AppSettings doesn’t exist for example, it will throw an exception and the type won’t initialize.
Run the program in the debugger, and set it to break on any managed exception and see what the problem actually is. Or rewrite your code to be more exception safe.