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. passing enum value to other method in different namespace

passing enum value to other method in different namespace

Scheduled Pinned Locked Moved C#
regexhelpquestion
6 Posts 4 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.
  • B Offline
    B Offline
    Blubbo
    wrote on last edited by
    #1

    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;
            }
        }
    }
    

    }

    P D P 3 Replies Last reply
    0
    • B Blubbo

      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;
              }
          }
      }
      

      }

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      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 simple using 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

      B 1 Reply Last reply
      0
      • B Blubbo

        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;
                }
            }
        }
        

        }

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

        junk.Output_State and junkie.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)

        1 Reply Last reply
        0
        • P Pete OHanlon

          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 simple using 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

          B Offline
          B Offline
          Blubbo
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • B Blubbo

            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;
                    }
                }
            }
            

            }

            P Offline
            P Offline
            pramod hegde
            wrote on last edited by
            #5

            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;
                    }
                }
            }
            

            }

            B 1 Reply Last reply
            0
            • P pramod hegde

              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;
                      }
                  }
              }
              

              }

              B Offline
              B Offline
              Blubbo
              wrote on last edited by
              #6

              Hmm... I like the suggestion you've made. I'll look into that in the future revision. Thanks!

              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