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. The type initializer threw an exception?

The type initializer threw an exception?

Scheduled Pinned Locked Moved C#
helpquestion
6 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.
  • D Offline
    D Offline
    dec82
    wrote on last edited by
    #1

    I have the class Class1: public enum Protocol { Digital, Analog } public enum Model { Digital1, Digital2, Analog1, Analog2 } public static Protocol m_selectedProtocol; public static Model m_ModelSelected; public static Protocol SelectedProtocol { get { return Class1.m_selectedProtocol; } set { Class1.m_selectedProtocol = value; } } public static Model ModelSelected { get { return Class1.m_m_ModelSelected ; } set { Class1.m_ModelSelected= value; } } Then here is the code being executed: if (rdAnalog.Checked == true) { Class1.SelectedProtocol = Protocol.Analog; if (Analog1.Checked == true) { Class1.ModelSelected = Model.Analog1; } else if (Analog2.Checked == true) { Class1.ModelSelected = Model.Analog2; } else { return; } else { return; } i have this error when this line is executed :Class1.SelectedProtocol = Protocol.Analog; How can it be solved ? thanks

    L S 3 Replies Last reply
    0
    • D dec82

      I have the class Class1: public enum Protocol { Digital, Analog } public enum Model { Digital1, Digital2, Analog1, Analog2 } public static Protocol m_selectedProtocol; public static Model m_ModelSelected; public static Protocol SelectedProtocol { get { return Class1.m_selectedProtocol; } set { Class1.m_selectedProtocol = value; } } public static Model ModelSelected { get { return Class1.m_m_ModelSelected ; } set { Class1.m_ModelSelected= value; } } Then here is the code being executed: if (rdAnalog.Checked == true) { Class1.SelectedProtocol = Protocol.Analog; if (Analog1.Checked == true) { Class1.ModelSelected = Model.Analog1; } else if (Analog2.Checked == true) { Class1.ModelSelected = Model.Analog2; } else { return; } else { return; } i have this error when this line is executed :Class1.SelectedProtocol = Protocol.Analog; How can it be solved ? thanks

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      The code you have presented (except of a coouple of typos) works -

      public class Class1
      {
      public enum Protocol
      {
      Digital,
      Analog
      }
      public enum Model
      {
      Digital1,
      Digital2,
      Analog1,
      Analog2
      }

      	public static Protocol m\_selectedProtocol;
      	public static Model m\_ModelSelected;
      
      	public static Protocol SelectedProtocol
      	{
      		get { return Class1.m\_selectedProtocol; }
      		set { Class1.m\_selectedProtocol = value; }
      	}
      	public static Model ModelSelected
      	{
      		get { return Class1.m\_ModelSelected ; }
      		set { Class1.m\_ModelSelected= value; }
      	}
      
      }
      
      	private void button1\_Click(object sender, EventArgs e)
      	{
      		Class1.SelectedProtocol = Class1.Protocol.Analog;
      	}
      

      so there is something in the code you have not posted causing the problem - are you able to post some complete code that shows the problem?

      ___________________________________________ .\\axxx (That's an 'M')

      D 1 Reply Last reply
      0
      • L Lost User

        The code you have presented (except of a coouple of typos) works -

        public class Class1
        {
        public enum Protocol
        {
        Digital,
        Analog
        }
        public enum Model
        {
        Digital1,
        Digital2,
        Analog1,
        Analog2
        }

        	public static Protocol m\_selectedProtocol;
        	public static Model m\_ModelSelected;
        
        	public static Protocol SelectedProtocol
        	{
        		get { return Class1.m\_selectedProtocol; }
        		set { Class1.m\_selectedProtocol = value; }
        	}
        	public static Model ModelSelected
        	{
        		get { return Class1.m\_ModelSelected ; }
        		set { Class1.m\_ModelSelected= value; }
        	}
        
        }
        
        	private void button1\_Click(object sender, EventArgs e)
        	{
        		Class1.SelectedProtocol = Class1.Protocol.Analog;
        	}
        

        so there is something in the code you have not posted causing the problem - are you able to post some complete code that shows the problem?

        ___________________________________________ .\\axxx (That's an 'M')

        D Offline
        D Offline
        dec82
        wrote on last edited by
        #3

        Here is the code : if (rdAnalog.Checked == true) { Class1.SelectedProtocol = Protocol.Analog; private void buttonStartTest_Click(object sender, EventArgs e) { try { if (Analog1.Checked == true) { Class1.ModelSelected = Model.Analog1; } else if (Analog2.Checked == true) { Class1.ModelSelected = Model.Analog2; } else { return; } else { return; } } catch (Exception c) { MessageBox.Show(c.Message); Environment.Exit(0); } }

        L 1 Reply Last reply
        0
        • D dec82

          I have the class Class1: public enum Protocol { Digital, Analog } public enum Model { Digital1, Digital2, Analog1, Analog2 } public static Protocol m_selectedProtocol; public static Model m_ModelSelected; public static Protocol SelectedProtocol { get { return Class1.m_selectedProtocol; } set { Class1.m_selectedProtocol = value; } } public static Model ModelSelected { get { return Class1.m_m_ModelSelected ; } set { Class1.m_ModelSelected= value; } } Then here is the code being executed: if (rdAnalog.Checked == true) { Class1.SelectedProtocol = Protocol.Analog; if (Analog1.Checked == true) { Class1.ModelSelected = Model.Analog1; } else if (Analog2.Checked == true) { Class1.ModelSelected = Model.Analog2; } else { return; } else { return; } i have this error when this line is executed :Class1.SelectedProtocol = Protocol.Analog; How can it be solved ? thanks

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          It seems that you have missed a '}' in your code. Changing your code like below is working

          if (rdAnalog.Checked == true)
          {
          Class1.SelectedProtocol = Protocol.Analog;

          if (Analog1.Checked == true)
          {
              Class1.ModelSelected = Model.Analog1;
          }
          else if (Analog2.Checked == true)
          {
              Class1.ModelSelected = Model.Analog2;
          }
          else
          {
              return;
          }
          

          } /*This Curly bracket you have missed */
          else
          {
          return;
          }

          1 Reply Last reply
          0
          • D dec82

            I have the class Class1: public enum Protocol { Digital, Analog } public enum Model { Digital1, Digital2, Analog1, Analog2 } public static Protocol m_selectedProtocol; public static Model m_ModelSelected; public static Protocol SelectedProtocol { get { return Class1.m_selectedProtocol; } set { Class1.m_selectedProtocol = value; } } public static Model ModelSelected { get { return Class1.m_m_ModelSelected ; } set { Class1.m_ModelSelected= value; } } Then here is the code being executed: if (rdAnalog.Checked == true) { Class1.SelectedProtocol = Protocol.Analog; if (Analog1.Checked == true) { Class1.ModelSelected = Model.Analog1; } else if (Analog2.Checked == true) { Class1.ModelSelected = Model.Analog2; } else { return; } else { return; } i have this error when this line is executed :Class1.SelectedProtocol = Protocol.Analog; How can it be solved ? thanks

            S Offline
            S Offline
            S Senthil Kumar
            wrote on last edited by
            #5

            One of the types involved probably has a static constructor, and I'm guessing that static constructor is throwing an exception. Put breakpoints in the static constructors of all classes involved, and step through them in the debugger.

            Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

            1 Reply Last reply
            0
            • D dec82

              Here is the code : if (rdAnalog.Checked == true) { Class1.SelectedProtocol = Protocol.Analog; private void buttonStartTest_Click(object sender, EventArgs e) { try { if (Analog1.Checked == true) { Class1.ModelSelected = Model.Analog1; } else if (Analog2.Checked == true) { Class1.ModelSelected = Model.Analog2; } else { return; } else { return; } } catch (Exception c) { MessageBox.Show(c.Message); Environment.Exit(0); } }

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              I'd need to see the code for Class1 - sorry if I wasn't clear. As someone says below, it could be a static constructor causing the issue - but without seeing the code for the class it's hard to guess.

              ___________________________________________ .\\axxx (That's an 'M')

              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