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 a property on Set

Validating a property on Set

Scheduled Pinned Locked Moved C#
tutorialquestioncombusinesstools
5 Posts 3 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.
  • M Offline
    M Offline
    martin_hughes
    wrote on last edited by
    #1

    My ability to google and comprehend things today appears to be not what it should - I've been up since 4am so go easy on me :) Anyway, suppose I have a very simple class with a single property:

    public string SiteName {
    get{return siteName;}
    set{siteName = value;}
    }

    Now SiteName has some very specific format requirements (ie not longer than 12 characters, first 4 chars are alpha only for example). Within the setter I can do some validation with a regex or whatever, and for validating this single property everything is great. It also seems to make sense to me that this is where the validation should take place. However, it occurs to me that I might want to do a similar kind of validation on another property, perhaps even within in a separate class - or even multiple classes, and could get very messy with the same (or very similar) logic appearing all over the place. This looks to me like an ideal candidate for re-factoring... and this is where my googling has come unstuck! What I'm really after is an insight on to tried an trusted techniques for validating properties, and also whether my intent is actually going to cause bigger problems than they're worth. I had a quick read about Custom Attributes, but I was not particularly clear on how to hook these up to know when a property is being modified, or how they interact with the property in question... As always, any help greatly appreciated.

    ***The collected future Mrs. Martin Hughes***

    P 1 Reply Last reply
    0
    • M martin_hughes

      My ability to google and comprehend things today appears to be not what it should - I've been up since 4am so go easy on me :) Anyway, suppose I have a very simple class with a single property:

      public string SiteName {
      get{return siteName;}
      set{siteName = value;}
      }

      Now SiteName has some very specific format requirements (ie not longer than 12 characters, first 4 chars are alpha only for example). Within the setter I can do some validation with a regex or whatever, and for validating this single property everything is great. It also seems to make sense to me that this is where the validation should take place. However, it occurs to me that I might want to do a similar kind of validation on another property, perhaps even within in a separate class - or even multiple classes, and could get very messy with the same (or very similar) logic appearing all over the place. This looks to me like an ideal candidate for re-factoring... and this is where my googling has come unstuck! What I'm really after is an insight on to tried an trusted techniques for validating properties, and also whether my intent is actually going to cause bigger problems than they're worth. I had a quick read about Custom Attributes, but I was not particularly clear on how to hook these up to know when a property is being modified, or how they interact with the property in question... As always, any help greatly appreciated.

      ***The collected future Mrs. Martin Hughes***

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Martin - take a look at using INotifyPropertyChanged and something like this[^] article.

      Deja View - the feeling that you've seen this post before.

      My blog | My articles

      M A 2 Replies Last reply
      0
      • P Pete OHanlon

        Martin - take a look at using INotifyPropertyChanged and something like this[^] article.

        Deja View - the feeling that you've seen this post before.

        My blog | My articles

        M Offline
        M Offline
        martin_hughes
        wrote on last edited by
        #3

        From the article: "Take note – in the screens above, the code that triggers the error to be shown is in the business object, not in the GUI. I didn't have to write a single line of GUI code – it's already done by data binding and IDataErrorInfo." Today's epiphany sorted :)

        ***The collected future Mrs. Martin Hughes***

        P 1 Reply Last reply
        0
        • M martin_hughes

          From the article: "Take note – in the screens above, the code that triggers the error to be shown is in the business object, not in the GUI. I didn't have to write a single line of GUI code – it's already done by data binding and IDataErrorInfo." Today's epiphany sorted :)

          ***The collected future Mrs. Martin Hughes***

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          Yup - binding's so cool - and so bloody useful.

          Deja View - the feeling that you've seen this post before.

          My blog | My articles

          1 Reply Last reply
          0
          • P Pete OHanlon

            Martin - take a look at using INotifyPropertyChanged and something like this[^] article.

            Deja View - the feeling that you've seen this post before.

            My blog | My articles

            A Offline
            A Offline
            anujarya_2001
            wrote on last edited by
            #5

            ou install the utility and navigate to any url in CP from where you can post a message, the signature field is automatically filled with one of the quotes from an XML file. All you need to do is to write some quotes in an XML file either manually or using the CodePr

            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