1

I was wondering is there is some C#\Visual Studio magic where I can have a list of strings that form a allow list for parameter inputs into a function?

In other words I would have a list of strings [“Green”, “yellow”, “blue”] and a function void example(string colour);. If I try to do example("red"); I get a complier error.

Bonus point if I can read this list for a text file or something, but copy and paste is fine.

Thanks

EDIT: I have ended up using const string C_RED = "RED". I never noticed that intellisence will give you a list of const. This works well as I can just type C_ and it will give me the valid options.

markdem
  • 51
  • 1
  • 8
  • 4
    It almost seem like you're looking for an [*enumeration*](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/enum)? – Some programmer dude Sep 27 '22 at 07:49
  • Find this: https://stackoverflow.com/questions/8588384/how-to-define-an-enum-with-string-value – CAD Developer Sep 27 '22 at 07:51
  • Sorry if this is stupid but would a enum not change the value to a int? If I did "example(Yellow);" would it not pass "1" as the parameter? I still need to pass the string to the function. – markdem Sep 27 '22 at 08:17
  • Can't you convert the enum parameter to a string inside the function? Although even if you were using an enum the compiler would allow you cast an invalid value to the enum without issuing a compile error, e.g. `MyFunc((Color)18797);` (assuming `Color` is an enum) – Matthew Watson Sep 27 '22 at 08:32
  • So that would not do what I need. I need to make sure the value is vaild before I complie (or run). – markdem Sep 27 '22 at 08:54
  • Why does it have to be *strings*? What is the *actual* and *underlying* problem you need to solve? This is really too much of an [XY problem](https://xyproblem.info/) in its current state. – Some programmer dude Sep 28 '22 at 05:50
  • It needs to be a string becuase they are been sent to a telnet session... – markdem Sep 28 '22 at 23:36

3 Answers3

0

Store the string in the list collection. Then take out the corresponding value and pass it to the example method.

  static void Main(string[] args)
    {
        List<string> mList = new List<string>(){"Green", "Rellow", "blue"};

        mList.Add("Green");
        mList.Add("Rellow");
        mList.Add("blue");
        new Program().example(mList[2]);

    }

   
    public void example(string colour)
    {
        Console.WriteLine("colour:" + colour);
    }

Hope it helps you.

Housheng-MSFT
  • 1
  • 1
  • 1
  • 9
0

A analogy of the problem I faced and this is how I addressed it. I want to restrict the Action in a Controller to be executed only if the query parameters in the request has allowed values.

1. Created a custom Validation Attribute

public class ElementValidate : ValidationAttribute
    {
        public string[] element = { "GREEN","YELLOW","BLUE" };

        public override bool IsValid(object? value)
        {
           string user_input =  (value as string).ToUpper();
            if (element.Contains(user_input))
            { 
                  return true;
            }
            else
                return false;   

        }
    }

2. Place the Attribute over the action method for which it is to be checked.

   [ElementValidate]
    public void Sample1()
    {
      // Actual Logic Goes Here
    }
Phaneendra
  • 103
  • 9
0

Try this

enum colors {Green=1, yellow=2, blue=3};

void example(string colour)
        {
            //Convert month in enum;
            Enum.TryParse(colour, out colors givenColour);

            if (givenColour == 0)
                throw new Exception("Invalid colur");

            // your implementation
        }

enter image description here

Pradeep Kumar
  • 1,193
  • 1
  • 9
  • 21