The type initializer threw an exception?
-
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
-
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
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')
-
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')
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); } }
-
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
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;
} -
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
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
-
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); } }
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')