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. What's the purpose to use Property with set and get to change a field?

What's the purpose to use Property with set and get to change a field?

Scheduled Pinned Locked Moved C#
questioncsharp
17 Posts 7 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.
  • H Hiren solanki

    Let me give you the strong need of property in C#. Suppose there's one field CustomerID and you have assigned CustomerIDProperty to get and set values for CustomerID. Now After a years requirements being changed and They say they need some validation on CustomerID now you can not change or not advisable to put a check of CustomerID everywhere in the code, At that time you can simply put check at set method of CustomerIDProperty to be rescued. Property is introduced because of the changing requirement in software field. "Walking on water and devloping a software both are the easy things,Provided both FROZEN".

    Regards, Hiren.

    My Recent Article: - Way to know which control have raised PostBack
    My Recent Tip/Trick: - Remove HTML Tag, get plain Text

    J Offline
    J Offline
    Jeff Connelly
    wrote on last edited by
    #8

    Hiren Solanki wrote:

    Let me give you the strong need of property in C#. ... Property is introduced because of the changing requirement in software field.

    Property was introduced just as a convenience, not because of any need. The change requirement could easily be implemented with normal methods as in C++. It's a quicker way to code for the developer, it's a quicker way to call for the consumer (no parentheses required), offers an auto-generation option, and signifies intent of the developer with fewer comments (a property is used when there is little calculation and no side effects). None of these are necessary though.

    P K 2 Replies Last reply
    0
    • J Jeff Connelly

      Hiren Solanki wrote:

      Let me give you the strong need of property in C#. ... Property is introduced because of the changing requirement in software field.

      Property was introduced just as a convenience, not because of any need. The change requirement could easily be implemented with normal methods as in C++. It's a quicker way to code for the developer, it's a quicker way to call for the consumer (no parentheses required), offers an auto-generation option, and signifies intent of the developer with fewer comments (a property is used when there is little calculation and no side effects). None of these are necessary though.

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

      Also that Interfaces can specify properties, but not fields.

      1 Reply Last reply
      0
      • J Jeff Connelly

        Hiren Solanki wrote:

        Let me give you the strong need of property in C#. ... Property is introduced because of the changing requirement in software field.

        Property was introduced just as a convenience, not because of any need. The change requirement could easily be implemented with normal methods as in C++. It's a quicker way to code for the developer, it's a quicker way to call for the consumer (no parentheses required), offers an auto-generation option, and signifies intent of the developer with fewer comments (a property is used when there is little calculation and no side effects). None of these are necessary though.

        K Offline
        K Offline
        kevinnicol
        wrote on last edited by
        #10

        It also gives hints at what to look for when reflecting through an object. IE Some asp.net data grids will auto bind columns to all properties of an object.

        J 1 Reply Last reply
        0
        • K kevinnicol

          It also gives hints at what to look for when reflecting through an object. IE Some asp.net data grids will auto bind columns to all properties of an object.

          J Offline
          J Offline
          Jeff Connelly
          wrote on last edited by
          #11

          kevinnicol wrote:

          Some asp.net data grids will auto bind columns to all properties of an object.

          That's true and good point, but that's a .NET issue that wasn't applicable to C++.

          modified on Friday, December 17, 2010 2:40 PM

          K 1 Reply Last reply
          0
          • J Jeff Connelly

            kevinnicol wrote:

            Some asp.net data grids will auto bind columns to all properties of an object.

            That's true and good point, but that's a .NET issue that wasn't applicable to C++.

            modified on Friday, December 17, 2010 2:40 PM

            K Offline
            K Offline
            kevinnicol
            wrote on last edited by
            #12

            oops, thought I was in the C# forums, must have stumbled into the C++ forums by accident.

            J 1 Reply Last reply
            0
            • K kevinnicol

              oops, thought I was in the C# forums, must have stumbled into the C++ forums by accident.

              J Offline
              J Offline
              Jeff Connelly
              wrote on last edited by
              #13

              kevinnicol wrote:

              oops, thought I was in the C# forums, must have stumbled into the C++ forums by accident

              The point is to answer in context of the OP's questions, and if you had taken the time you spent on your clever sarcasm and spent it on reading all the OP's posts instead, you'd know he was coming from an OO background from C++. So I think you need to decide if you want to help someone with a question in context, or if you want to show off your mad skilz :) The OP said "I am wondering if that is a Microsoft's "invention" or a general OOP thinking. I never met it in C++." By way of my response to you, I was telling him that data grid data binding with properties is a Microsoft-specific invention for C#/.NET, and not general OOP thinking. I replied to your post to help him and the lurkers, not educate you. But as a token of goodwill, I'm still willing to educate you. You misused "IE" in your previous post. :) You might want to look that one up.

              K 1 Reply Last reply
              0
              • J Jeff Connelly

                kevinnicol wrote:

                oops, thought I was in the C# forums, must have stumbled into the C++ forums by accident

                The point is to answer in context of the OP's questions, and if you had taken the time you spent on your clever sarcasm and spent it on reading all the OP's posts instead, you'd know he was coming from an OO background from C++. So I think you need to decide if you want to help someone with a question in context, or if you want to show off your mad skilz :) The OP said "I am wondering if that is a Microsoft's "invention" or a general OOP thinking. I never met it in C++." By way of my response to you, I was telling him that data grid data binding with properties is a Microsoft-specific invention for C#/.NET, and not general OOP thinking. I replied to your post to help him and the lurkers, not educate you. But as a token of goodwill, I'm still willing to educate you. You misused "IE" in your previous post. :) You might want to look that one up.

                K Offline
                K Offline
                kevinnicol
                wrote on last edited by
                #14

                There was no sarcasm intended, I just saw your reply in my email and truly thought I had made a reply in the wrong forum. There was no intent of sarcasm or showing of my "mad skilz". I will choose to take your token of goodwill as that and not passive aggression; in the future will use eg. instead of ie. in those cases. Thanks.

                J 1 Reply Last reply
                0
                • K kevinnicol

                  There was no sarcasm intended, I just saw your reply in my email and truly thought I had made a reply in the wrong forum. There was no intent of sarcasm or showing of my "mad skilz". I will choose to take your token of goodwill as that and not passive aggression; in the future will use eg. instead of ie. in those cases. Thanks.

                  J Offline
                  J Offline
                  Jeff Connelly
                  wrote on last edited by
                  #15

                  kevinnicol wrote:

                  There was no sarcasm intended

                  Oh sorry, my bad! :doh: Since you already know about e.g., you're ahead of 90% of the people out there....

                  T 1 Reply Last reply
                  0
                  • J Jeff Connelly

                    kevinnicol wrote:

                    There was no sarcasm intended

                    Oh sorry, my bad! :doh: Since you already know about e.g., you're ahead of 90% of the people out there....

                    T Offline
                    T Offline
                    Toli Cuturicu
                    wrote on last edited by
                    #16

                    Everybody knows about exempli gratia and id est. They are learned in elementary school.

                    J 1 Reply Last reply
                    0
                    • T Toli Cuturicu

                      Everybody knows about exempli gratia and id est. They are learned in elementary school.

                      J Offline
                      J Offline
                      Jeff Connelly
                      wrote on last edited by
                      #17

                      ha ha yeah, right! If I only had a nickel for every time i.e. got misused.....

                      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