3

I have a HW data bit clock I would like to set to different modes in C#, but I am wondering the best way to implement this. Here is my old implementation with strings, which worked fine as both projects know what a string is, but using strings and string compares seems error prone, so I switched to enums.

int SetDAI_BCLK(int32 Hz, string mode)
{
     // int32 Hz Specifies the BCLK rate in Hz
     // string mode: "master" => master mode, "slave" => slave mode, "off" => clock off, "poll1" => special polling mode

     // Implementation here, use str compares to set the clock mode

     // return 1 if success, -1 if comm error, -2 if invalid clock settings, -3 if invalid string master
}

The enums work great if it's all in the same project, but won't work across projects, whereas the strings will. I can't figure out how to do it without hardcoding the enums in both projects.

Project A contains the enum definitions and references project B which contains the equipment functions. Project B doesn't know of these enums, and when I try to reference project A, I get a circular reference error and am not allowed to reference it. Is there anyway to reference just the one file? What's the best way to use the same enums across these two projects. Using VS 2013 Express.

Project B

namespace EquipControl
{
    public interface IMotherboard
    {
        int connectOutput_to_DUT(EAPOutput APOutput);
    }

    public class HWMotherBoard : IMotherboard
    {
        public int connectOutput_to_DUT(EAPOutput APOutput)
        {
            if (APOutput == EAPOutput.AnalogStereo)
            {
                //set stereo
            }
        }
    }
}

Project A

using EquipControl;

namespace TestSuite
{
    public partial class test1
    {
        public enum EAPOutput
        {
            AnalogMonoL,
            AnalogMonoR,
            AnalogStereo
        }

        public EAPOutput APOutput = EAPOutput.AnalogStereo;

        public void main()
        {
            HWMotherBoard HWMB = new HWMotherBoard();
            HWMB.connectOutput_to_DUT(APOutput);
        }
    }
}
SwimBikeRun
  • 4,192
  • 11
  • 49
  • 85
  • 4
    Why not move the enum def to "B"? – BradleyDotNET Feb 12 '15 at 20:06
  • Is your `enum EAPOutput` supposed to be public as well? – ryanyuyu Feb 12 '15 at 20:06
  • Why not put the enums in Project B instead? Project A already references to it, and your main project should not reference your test suite, ever, anyway. – Pierre-Luc Pineault Feb 12 '15 at 20:08
  • As an aside, hardcoding the same Enum in two assemblies won't work - you'll end up with two enums with the same namespace and name but defined in two different assemblies, and will be treated as different. For instance, [this SO question](http://stackoverflow.com/q/453451/1364007), or [this MSDN article](https://msdn.microsoft.com/en-us/library/ms973231.aspx) (see the Type Identity) section). – Wai Ha Lee Feb 12 '15 at 20:13

1 Answers1

6

Put the enums in the bottom-most project (the one that is referenced by the other) and they'll be available to both projects. Also, make the enums public, and you might want to define them outside of a class definition.

adv12
  • 8,443
  • 2
  • 24
  • 48
  • Unfortunately, it doesn't make organizational sense to do that (for my project, otherwise this is a great answer). I guess I should put the enums in a 3rd project and reference it in the other two. – SwimBikeRun Feb 12 '15 at 21:04