Validating string values
-
Hi, Is there a rather simple and constructive ways of validating a string value against a bunch value? For example, If I have a variable called "strStatus" it should only hold "In-Progress", "Closed" or "Open". I know that I can do this using simple if condition or linq. Is there any other ways, like using enum etc.
Jack Sparrow -------------------------------------- Defeat is not the worst of failures. Not to have tried is the true failure.
-
Hi, Is there a rather simple and constructive ways of validating a string value against a bunch value? For example, If I have a variable called "strStatus" it should only hold "In-Progress", "Closed" or "Open". I know that I can do this using simple if condition or linq. Is there any other ways, like using enum etc.
Jack Sparrow -------------------------------------- Defeat is not the worst of failures. Not to have tried is the true failure.
If the variable is only allowed to hold certain set of values, I wouldn't use string at all... An enum is the way to go. Enums can be converted to string easily and you can also parse a string to get the corresponding enum value. If, for some reason you really need to use a string, there are several ways to validate it. Using a regular expression might be a good way to do it...
-
If the variable is only allowed to hold certain set of values, I wouldn't use string at all... An enum is the way to go. Enums can be converted to string easily and you can also parse a string to get the corresponding enum value. If, for some reason you really need to use a string, there are several ways to validate it. Using a regular expression might be a good way to do it...
Yes an Enum should do the trick.
Jack Sparrow -------------------------------------- Defeat is not the worst of failures. Not to have tried is the true failure.
-
Yes an Enum should do the trick.
Jack Sparrow -------------------------------------- Defeat is not the worst of failures. Not to have tried is the true failure.
To add to that, have a look atTips: Human readable strings for enum elements[^] which will let you use "In-Progress" as the string for the enum (normally you can't as enum elements must conform to normal C# variable name rules)
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
-
Hi, Is there a rather simple and constructive ways of validating a string value against a bunch value? For example, If I have a variable called "strStatus" it should only hold "In-Progress", "Closed" or "Open". I know that I can do this using simple if condition or linq. Is there any other ways, like using enum etc.
Jack Sparrow -------------------------------------- Defeat is not the worst of failures. Not to have tried is the true failure.
Yes, an enumeration and my EnumTransmogrifier[^].