0

I am trying to find what is the most elegant way to handle the below situation:

class ObjectA
{
    public enum MandatoryParameters {A, B, C}
    public enum OptionalParameters {D, E, F}
}
class ObjectB
{
    public enum MandatoryParameters {G, H, I}
    public enum OptionalParameters {J, K}
}
class Error
{
    MandatoryOrOptionalParameters param;
    string errorDescription;

    Error(MandatoryOrOptionalParameters p, string d)
    {
        param = p;
        errorDescription = d;
    }
}

Is there any good way to create a MandatoryOrOptionalParameters type to avoid string? I read the few related message here and here.

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
Salim
  • 495
  • 3
  • 20
  • 1
    Sounds like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), where your solution Y would be _"I know, I'll use enums"_, which is a suboptimal solution for problem X to begin with. Explain why you chose enums in the first place, and how you're going to use them, so perhaps a better design altogether can be suggested. – CodeCaster Aug 27 '17 at 07:07
  • Check flags attribute for enum type – sTrenat Aug 27 '17 at 07:12
  • Smart comment CodeCaster :-) I had this feeling when posting. The enum values represent the object attributes which are filled during the serialization process. I am using the enum types for 1) loop through mandatory and check if null value => raise error, 2) create a list of validation errors to be displayed in the form of Field/ErrorDescription. – Salim Aug 27 '17 at 07:18

1 Answers1

2

Create an enum representing any kind of parameters and then each class will get or specify a collection of items representing the optional or the mandatory:

public enum Parameters { A, B, C, D, E }

// Class specifies for itself the mandatory and optional
public class Foo
{
    public IEnumerable<Parameters> Mandatory { get; set; } = new List<Parameters> { A, B };

    public IEnumerable<Parameters> Optional { get; set; } = new List<Parameters> { C, D };
}

// Class receives what are the mandatory and optional
public class Bar
{
    public Bar(IEnumerable<Parameter> optional, IEnumerable<Parameter> mandatory)
    {
        // store input
    }
}
Gilad Green
  • 36,708
  • 7
  • 61
  • 95