passing enum value to other method in different namespace
-
I'm not sure what I've gone wrong. It works ok in same namespace but not in different namespace. I use the dll calling to pass the enum value. The compiler spat out 2 errors: Error 1: The best overloaded method match for 'junkie.NewClass1.TestMethod(junkie.Output_State)' has some invalid arguments Error 2: Argument 1: cannot convert from 'junk.Output_State' to 'junkie.Output_State' sample code that I've written:
using junkie;
namespace junk
{
public enum Output_State
{
OFF,
ON,
Dont_Care,
}public partial class Form1 : Form { public Form1() { InitializeComponent(); } public void button1\_Click(object sender, EventArgs e) { ClassTwo testClassTwo = new ClassTwo(); testClassTwo.TestMethod(Output\_State.Dont\_Care); } } public class ClassTwo { junkie.NewClass1 testClassThree = new junkie.NewClass1(); public void TestMethod(Output\_State output) { testClassThree.TestMethod(output); } }
}
in a new class file:
namespace junkie
{
public enum Output_State
{
OFF,
ON,
Dont_Care,
}class NewClass1 { public void TestMethod(Output\_State output) { switch (output) { case Output\_State.ON: MessageBox.Show("ON"); break; case Output\_State.OFF: MessageBox.Show("OFF"); break; case Output\_State.Dont\_Care: MessageBox.Show("Don't Care"); break; default: MessageBox.Show("HUH?"); break; } } }
}
-
I'm not sure what I've gone wrong. It works ok in same namespace but not in different namespace. I use the dll calling to pass the enum value. The compiler spat out 2 errors: Error 1: The best overloaded method match for 'junkie.NewClass1.TestMethod(junkie.Output_State)' has some invalid arguments Error 2: Argument 1: cannot convert from 'junk.Output_State' to 'junkie.Output_State' sample code that I've written:
using junkie;
namespace junk
{
public enum Output_State
{
OFF,
ON,
Dont_Care,
}public partial class Form1 : Form { public Form1() { InitializeComponent(); } public void button1\_Click(object sender, EventArgs e) { ClassTwo testClassTwo = new ClassTwo(); testClassTwo.TestMethod(Output\_State.Dont\_Care); } } public class ClassTwo { junkie.NewClass1 testClassThree = new junkie.NewClass1(); public void TestMethod(Output\_State output) { testClassThree.TestMethod(output); } }
}
in a new class file:
namespace junkie
{
public enum Output_State
{
OFF,
ON,
Dont_Care,
}class NewClass1 { public void TestMethod(Output\_State output) { switch (output) { case Output\_State.ON: MessageBox.Show("ON"); break; case Output\_State.OFF: MessageBox.Show("OFF"); break; case Output\_State.Dont\_Care: MessageBox.Show("Don't Care"); break; default: MessageBox.Show("HUH?"); break; } } }
}
The reason you are having a problem is because these aren't the same enums. By that, I mean that you have actually declared two enums here - the fact that they have the same names means nothing. What you need to do is drop the enum from the
junkie
namespace, and then add a simpleusing junk;
statement to junkie. It will look like this:namespace junkie
{
using junk;
public class NewClass1
{
public void TestMethod(Output_State output)
{
// Do your work here....
}
}
}The using statement means that the
junk.Output_State
enumeration is used.*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
-
I'm not sure what I've gone wrong. It works ok in same namespace but not in different namespace. I use the dll calling to pass the enum value. The compiler spat out 2 errors: Error 1: The best overloaded method match for 'junkie.NewClass1.TestMethod(junkie.Output_State)' has some invalid arguments Error 2: Argument 1: cannot convert from 'junk.Output_State' to 'junkie.Output_State' sample code that I've written:
using junkie;
namespace junk
{
public enum Output_State
{
OFF,
ON,
Dont_Care,
}public partial class Form1 : Form { public Form1() { InitializeComponent(); } public void button1\_Click(object sender, EventArgs e) { ClassTwo testClassTwo = new ClassTwo(); testClassTwo.TestMethod(Output\_State.Dont\_Care); } } public class ClassTwo { junkie.NewClass1 testClassThree = new junkie.NewClass1(); public void TestMethod(Output\_State output) { testClassThree.TestMethod(output); } }
}
in a new class file:
namespace junkie
{
public enum Output_State
{
OFF,
ON,
Dont_Care,
}class NewClass1 { public void TestMethod(Output\_State output) { switch (output) { case Output\_State.ON: MessageBox.Show("ON"); break; case Output\_State.OFF: MessageBox.Show("OFF"); break; case Output\_State.Dont\_Care: MessageBox.Show("Don't Care"); break; default: MessageBox.Show("HUH?"); break; } } }
}
junk.Output_State
andjunkie.Output_State
are two different types, even though they appear identical in all other respects.Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
The reason you are having a problem is because these aren't the same enums. By that, I mean that you have actually declared two enums here - the fact that they have the same names means nothing. What you need to do is drop the enum from the
junkie
namespace, and then add a simpleusing junk;
statement to junkie. It will look like this:namespace junkie
{
using junk;
public class NewClass1
{
public void TestMethod(Output_State output)
{
// Do your work here....
}
}
}The using statement means that the
junk.Output_State
enumeration is used.*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
that what you suggested is good... but I think I would use the better way is to drop the enum in 'junk' namespace and reads as follows (no change in 2nd class file):
using junkie;
namespace junk
{
public partial class Form1 : Form
{
SerialPort _port;public Form1() { InitializeComponent(); } public void button1\_Click(object sender, EventArgs e) { ClassTwo testClassTwo = new ClassTwo(); testClassTwo.TestMethod(junkie.Output\_State.Dont\_Care); } } public class ClassTwo { junkie.NewClass1 testClassThree = new junkie.NewClass1(); public void TestMethod(junkie.Output\_State output) { testClassThree.TestMethod(output); } }
}
Thanks for a big help... I've learned something new.
-
I'm not sure what I've gone wrong. It works ok in same namespace but not in different namespace. I use the dll calling to pass the enum value. The compiler spat out 2 errors: Error 1: The best overloaded method match for 'junkie.NewClass1.TestMethod(junkie.Output_State)' has some invalid arguments Error 2: Argument 1: cannot convert from 'junk.Output_State' to 'junkie.Output_State' sample code that I've written:
using junkie;
namespace junk
{
public enum Output_State
{
OFF,
ON,
Dont_Care,
}public partial class Form1 : Form { public Form1() { InitializeComponent(); } public void button1\_Click(object sender, EventArgs e) { ClassTwo testClassTwo = new ClassTwo(); testClassTwo.TestMethod(Output\_State.Dont\_Care); } } public class ClassTwo { junkie.NewClass1 testClassThree = new junkie.NewClass1(); public void TestMethod(Output\_State output) { testClassThree.TestMethod(output); } }
}
in a new class file:
namespace junkie
{
public enum Output_State
{
OFF,
ON,
Dont_Care,
}class NewClass1 { public void TestMethod(Output\_State output) { switch (output) { case Output\_State.ON: MessageBox.Show("ON"); break; case Output\_State.OFF: MessageBox.Show("OFF"); break; case Output\_State.Dont\_Care: MessageBox.Show("Don't Care"); break; default: MessageBox.Show("HUH?"); break; } } }
}
If these enums are not going to change over time and they are similar, I suggest you to have it in a common namespace and access it in both places. like,
namespace Common
{
public enum Output_State
{
OFF,
ON,
Dont_Care,
}
}namespace junk
{
using junkie;
using Common;public partial class Form1 : Form { public Form1() { InitializeComponent(); } public void button1\_Click(object sender, EventArgs e) { ClassTwo testClassTwo = new ClassTwo(); testClassTwo.TestMethod(Output\_State.Dont\_Care); } } public class ClassTwo { junkie.NewClass1 testClassThree = new junkie.NewClass1(); public void TestMethod(Output\_State output) { testClassThree.TestMethod(output); } }
}
namespace junkie
{
using Common;class NewClass1 { public void TestMethod(Output\_State output) { switch (output) { case Output\_State.ON: MessageBox.Show("ON"); break; case Output\_State.OFF: MessageBox.Show("OFF"); break; case Output\_State.Dont\_Care: MessageBox.Show("Don't Care"); break; default: MessageBox.Show("HUH?"); break; } } }
}
-
If these enums are not going to change over time and they are similar, I suggest you to have it in a common namespace and access it in both places. like,
namespace Common
{
public enum Output_State
{
OFF,
ON,
Dont_Care,
}
}namespace junk
{
using junkie;
using Common;public partial class Form1 : Form { public Form1() { InitializeComponent(); } public void button1\_Click(object sender, EventArgs e) { ClassTwo testClassTwo = new ClassTwo(); testClassTwo.TestMethod(Output\_State.Dont\_Care); } } public class ClassTwo { junkie.NewClass1 testClassThree = new junkie.NewClass1(); public void TestMethod(Output\_State output) { testClassThree.TestMethod(output); } }
}
namespace junkie
{
using Common;class NewClass1 { public void TestMethod(Output\_State output) { switch (output) { case Output\_State.ON: MessageBox.Show("ON"); break; case Output\_State.OFF: MessageBox.Show("OFF"); break; case Output\_State.Dont\_Care: MessageBox.Show("Don't Care"); break; default: MessageBox.Show("HUH?"); break; } } }
}