Alex Gordon
Alex Gordon

Reputation: 60731

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>?

Upvotes: 1

Views: 1315

Answers (7)

devdigital
devdigital

Reputation: 34349

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

Upvotes: 2

slandau
slandau

Reputation: 24052

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());
    }
}

Upvotes: 3

Mark Schadler
Mark Schadler

Reputation: 126

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.

Upvotes: 1

Variant
Variant

Reputation: 17365

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

Upvotes: 1

Ed Bayiates
Ed Bayiates

Reputation: 11210

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.

Upvotes: 4

BrokenGlass
BrokenGlass

Reputation: 160892

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.

Upvotes: 1

Steve Morgan
Steve Morgan

Reputation: 13091

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.

Upvotes: 1

Related Questions