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. Validation techneques?

Validation techneques?

Scheduled Pinned Locked Moved C#
debuggingtutorialquestion
6 Posts 5 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.
  • V Offline
    V Offline
    venomation
    wrote on last edited by
    #1

    I have decided that I need a better way of validating data. I currently have various objects that can validate information, for example:

    if(TextBoxValidator.IsValidTextBox(tbText))...

    Also I tend to use the Debug.Assert() methods often, making sure that null or invalid states of variables are shown. Its starting to get repetitive when I validate:

    if(fileDetails.ToString() == string.Empty || map == null) return Common.ReturnResult.Failed;
    if(map.StaticImages == null || map.Tiles == null) return Common.ReturnResult.Failed;
    if(map.Infomation.Columns == 0 || map.Infomation.RootContentFolder == string.Empty || map.Infomation.Rows == 0 || map.Infomation.TileSize == 0)
    return Common.ReturnResult.Failed;

    There must be a better way to validate methods input and such than this! :-D Any suggestions, Thanks :doh:

    K D B M 4 Replies Last reply
    0
    • V venomation

      I have decided that I need a better way of validating data. I currently have various objects that can validate information, for example:

      if(TextBoxValidator.IsValidTextBox(tbText))...

      Also I tend to use the Debug.Assert() methods often, making sure that null or invalid states of variables are shown. Its starting to get repetitive when I validate:

      if(fileDetails.ToString() == string.Empty || map == null) return Common.ReturnResult.Failed;
      if(map.StaticImages == null || map.Tiles == null) return Common.ReturnResult.Failed;
      if(map.Infomation.Columns == 0 || map.Infomation.RootContentFolder == string.Empty || map.Infomation.Rows == 0 || map.Infomation.TileSize == 0)
      return Common.ReturnResult.Failed;

      There must be a better way to validate methods input and such than this! :-D Any suggestions, Thanks :doh:

      K Offline
      K Offline
      Keith Barrow
      wrote on last edited by
      #2

      One way is to avoid nulls in the first place, but if you need to check , here is a start, it is longer but clearer to read:

      ...

      bool Success(footype map) //This isn't the best name
      {
      if(map.StaticImages == null)
      return false;
      if(map.Tiles == null)
      return false;
      if(map.Information.RootContentFolder == "")
      return false;
      if(map.Infomation.Rows == 0)
      return false;
      if(map.Infomation.TileSize == 0)
      return false;
      }

      public Foo()
      {
      if(fileDetails.ToString() == string.Empty)
      return Common.ReturnResult.Failed;
      if(map == null)
      return Common.ReturnResult.Failed;
      if(!Success(map))
      return Common.ReturnResult.Failed;
      return Common.ReturnResult.OKorWhatever;

      }

      Can you change the map object? If so the Success Status is probably better off there as a property, you could also break this down into further sub-properties such as HasImages, HasInformation etc

      Sort of a cross between Lawrence of Arabia and Dilbert.[^]
      -Or-
      A Dead ringer for Kate Winslett[^]

      1 Reply Last reply
      0
      • V venomation

        I have decided that I need a better way of validating data. I currently have various objects that can validate information, for example:

        if(TextBoxValidator.IsValidTextBox(tbText))...

        Also I tend to use the Debug.Assert() methods often, making sure that null or invalid states of variables are shown. Its starting to get repetitive when I validate:

        if(fileDetails.ToString() == string.Empty || map == null) return Common.ReturnResult.Failed;
        if(map.StaticImages == null || map.Tiles == null) return Common.ReturnResult.Failed;
        if(map.Infomation.Columns == 0 || map.Infomation.RootContentFolder == string.Empty || map.Infomation.Rows == 0 || map.Infomation.TileSize == 0)
        return Common.ReturnResult.Failed;

        There must be a better way to validate methods input and such than this! :-D Any suggestions, Thanks :doh:

        D Offline
        D Offline
        Dalek Dave
        wrote on last edited by
        #3

        May be better to use CASE statement.

        ------------------------------------ I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave CCC League Table Link CCC Link[^]

        1 Reply Last reply
        0
        • V venomation

          I have decided that I need a better way of validating data. I currently have various objects that can validate information, for example:

          if(TextBoxValidator.IsValidTextBox(tbText))...

          Also I tend to use the Debug.Assert() methods often, making sure that null or invalid states of variables are shown. Its starting to get repetitive when I validate:

          if(fileDetails.ToString() == string.Empty || map == null) return Common.ReturnResult.Failed;
          if(map.StaticImages == null || map.Tiles == null) return Common.ReturnResult.Failed;
          if(map.Infomation.Columns == 0 || map.Infomation.RootContentFolder == string.Empty || map.Infomation.Rows == 0 || map.Infomation.TileSize == 0)
          return Common.ReturnResult.Failed;

          There must be a better way to validate methods input and such than this! :-D Any suggestions, Thanks :doh:

          B Offline
          B Offline
          Bernhard Hiller
          wrote on last edited by
          #4

          What about an "IValidable" interface defining a method "Validate()"? When "map" and its properties implement it, you could simply call map.Validate() which would encapsulate that logic there.

          V 1 Reply Last reply
          0
          • B Bernhard Hiller

            What about an "IValidable" interface defining a method "Validate()"? When "map" and its properties implement it, you could simply call map.Validate() which would encapsulate that logic there.

            V Offline
            V Offline
            venomation
            wrote on last edited by
            #5

            Thanks for the reply,the problem is that I would have allot of validation objects everywhere, which wouldn't be very fun to use...

            1 Reply Last reply
            0
            • V venomation

              I have decided that I need a better way of validating data. I currently have various objects that can validate information, for example:

              if(TextBoxValidator.IsValidTextBox(tbText))...

              Also I tend to use the Debug.Assert() methods often, making sure that null or invalid states of variables are shown. Its starting to get repetitive when I validate:

              if(fileDetails.ToString() == string.Empty || map == null) return Common.ReturnResult.Failed;
              if(map.StaticImages == null || map.Tiles == null) return Common.ReturnResult.Failed;
              if(map.Infomation.Columns == 0 || map.Infomation.RootContentFolder == string.Empty || map.Infomation.Rows == 0 || map.Infomation.TileSize == 0)
              return Common.ReturnResult.Failed;

              There must be a better way to validate methods input and such than this! :-D Any suggestions, Thanks :doh:

              M Offline
              M Offline
              Matty22
              wrote on last edited by
              #6

              HOw about public bool CheckForNulls(object[] thingsThatShouldntBeNull) { return thingsThatShouldntBeNull.TrueForAll(x => x != null); } CheckForNulls(new object[]{dumbshitCoderField1,dumbShitCoderField2);

              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