SwimBikeRun
SwimBikeRun

Reputation: 4460

Using enum across projects in C#

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

Upvotes: 3

Views: 2967

Answers (1)

adv12
adv12

Reputation: 8551

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.

Upvotes: 6

Related Questions