简体   繁体   中英

c# implementing a class to just hold variables

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.

If you're accessing the class that way everytime, I would make it static:

public static class GetColumnsNames

And give your properties getter options:

public static int Occurrence_Date 
{
    get
    {
        return Convert.ToInt16(ConfigurationSettings.AppSettings["Occurrence_Date"].ToString());
    }
}

One of your field initializations is throwing an error, probably a typo on one of the config setting names (in which case you're calling ToString() on an uninitialised object instance), or an invalid int conversion etc. Run in debug mode to track down the error.

One much cleaner option is to use custom configuration management

An initialiser for one (or more) of your members is throwing an exception.

At a guess, it's because one of them can't be parsed as an Int16. Either because there's no value in the AppSettings or because it's not in the right format.

Most likely one of your configuration settings does not exist or is not of the desired type, so that throws an exception - any uncaught exception will cause the type initialization to fail hence you see this error.

As it sais, There was an exception thrown during the static initialization code for the class.

My guess is that one of the AppSettings values is empty or not an integer value and the Convert.ToInt16 threw the exception

Mostly likely one of our AppSettings are missing. Thus doing AppSetting["setting"] is returning null. Then calling ToString on the null values causes initialization problems. Verify all of our AppSettings actually exist in the project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM