1

I want to have a string which is limited to 4-5 values (ex: "Insert", "update", "delete", "check") but no other values expect the above can be assigned to the string (similar to true and false in bool) but I want to know if it is possible without checking if the string is one of those values like so:

//not what i want
switch(str){
case "update":
isgoodvul=true;
break;
case "delete":
//(and so on).....
}

but more like

//what i want
string limited="hello world";
//error;
string limited="update";
//not error;
string any_other_string_name_exept_limited="hello world";
//not error;
string any_other_string_name_exept_limited="update";
//not error;
  • 1
    Have you considered using an Enum? You can parse strings to get the equivalent Enum value with relative ease. – JayV Jun 26 '19 at 08:16
  • 1
    Sounds more like an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) to me. What you are trying to achieve? – SᴇM Jun 26 '19 at 08:18
  • @JayV what is Enum and how to use it? –  Jun 26 '19 at 08:19
  • 3
    @avivgood1 Googe that maybe? But instead, I will recommend you to learn some basics of the language you are going to use. – SᴇM Jun 26 '19 at 08:20
  • @SeM Im trying to build a func that create SQL statements for you –  Jun 26 '19 at 08:20
  • @avivgood1: why not use an existing ORM, such as Entity Framework? – Mark Cilia Vincenti Jun 26 '19 at 08:21
  • @avivgood1 [enum (C# Reference)](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/enum) and https://www.google.co.uk/search?q=what+is+an+enum+c%23 – JayV Jun 26 '19 at 08:21

2 Answers2

0

Create a HashSet to store your whitelist and check whether the input is in the whitelist or not, as below:

var whitelist = new HashSet<string>()
{
    "insert",
    "update",
    "delete",
    "check"
};

And then use as below:

bool isgoodvul = whitelist.Contains(str);
Mark Cilia Vincenti
  • 1,410
  • 8
  • 25
0

You can use enums to get the behavior that you want. Define this:

enum MyAction
{
    insert = 0,
    update =1,
    delete = 2,
    check = 3
}

Then use it as such:

Enum action = MyAction.update;

switch(action){
  case(MyAction.insert)
    //Do insert
  case(MyAction.update)
    //Do update
...
}
Elias N
  • 1,430
  • 11
  • 19
  • how do I insert a new value to Enum action? –  Jun 26 '19 at 08:30
  • 1
    @MarkCiliaVincenti But I agree with `switch` though. – SᴇM Jun 26 '19 at 08:38
  • @MarkCillaVincenti Flags should only be used when the numerical values of the enums are a power of 2 and the enumerable represents a collection of possible values, rather than a single value. Check [this](https://stackoverflow.com/questions/8447/what-does-the-flags-enum-attribute-mean-in-c) and [this](https://learn.microsoft.com/en-us/visualstudio/code-quality/ca2217-do-not-mark-enums-with-flagsattribute?view=vs-2019). In the link that you referenced, the efficiency is about using bitwise operations over hashsets – Elias N Jun 26 '19 at 08:38
  • @avivgood1, just add a `,whatever = 4` to the `enum MyAction`. And I suggest you use `switch` instead of `if... else if`. – Mark Cilia Vincenti Jun 26 '19 at 08:39