Static Constructor
-
I have a class with a static constructor. First time I access the class the static constructor gets executed and there an exception occurs. After that if I try to access any of the methods of the class I am getting the same exception that was occured in the constructor. public class Test1 { static Test1() { //some exception here - ExceptionTest } public static void LoadData() { //if I access this method then I am getting ExceptionTest everytime I call this method. //The type initializer for 'X.Test1' threw an exception. } } How do I get rid of this? Thanks Shubho
-
I have a class with a static constructor. First time I access the class the static constructor gets executed and there an exception occurs. After that if I try to access any of the methods of the class I am getting the same exception that was occured in the constructor. public class Test1 { static Test1() { //some exception here - ExceptionTest } public static void LoadData() { //if I access this method then I am getting ExceptionTest everytime I call this method. //The type initializer for 'X.Test1' threw an exception. } } How do I get rid of this? Thanks Shubho
Static constructor is used to initialize static data members as soon as the class is referenced first time Go Through this link !!. It will help you Static Constructor in C#[^]
Best Regards ----------------- Abhijit Jana View My CodeProject Articles "Success is Journey it's not a destination"
-
I have a class with a static constructor. First time I access the class the static constructor gets executed and there an exception occurs. After that if I try to access any of the methods of the class I am getting the same exception that was occured in the constructor. public class Test1 { static Test1() { //some exception here - ExceptionTest } public static void LoadData() { //if I access this method then I am getting ExceptionTest everytime I call this method. //The type initializer for 'X.Test1' threw an exception. } } How do I get rid of this? Thanks Shubho
What is the exception? Copy the exception type, the exception message, and the line of code associated with that exception so we can help. I can say, that the reason you are getting an exception later on is solely due to the problem that is created within your static constructor. Any object that throws an exception while being constructed cannot be used anywhere in the code.
-Jeff