Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Static Constructor

Static Constructor

Scheduled Pinned Locked Moved C#
question
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Shubhabrata Mohanty
    wrote on last edited by
    #1

    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

    A S 2 Replies Last reply
    0
    • S Shubhabrata Mohanty

      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

      A Offline
      A Offline
      Abhijit Jana
      wrote on last edited by
      #2

      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"

      1 Reply Last reply
      0
      • S Shubhabrata Mohanty

        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

        S Offline
        S Offline
        Skippums
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups