Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Validating string values

Validating string values

Scheduled Pinned Locked Moved C#
csharplinqtutorialquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Ramkithepower
    wrote on last edited by
    #1

    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.

    K P 2 Replies Last reply
    0
    • R Ramkithepower

      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.

      K Offline
      K Offline
      Kubajzz
      wrote on last edited by
      #2

      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...

      R 1 Reply Last reply
      0
      • K Kubajzz

        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...

        R Offline
        R Offline
        Ramkithepower
        wrote on last edited by
        #3

        Yes an Enum should do the trick.

        Jack Sparrow -------------------------------------- Defeat is not the worst of failures. Not to have tried is the true failure.

        OriginalGriffO 1 Reply Last reply
        0
        • R Ramkithepower

          Yes an Enum should do the trick.

          Jack Sparrow -------------------------------------- Defeat is not the worst of failures. Not to have tried is the true failure.

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          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.

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          1 Reply Last reply
          0
          • R Ramkithepower

            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.

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            Yes, an enumeration and my EnumTransmogrifier[^].

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups