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. why use property in class?

why use property in class?

Scheduled Pinned Locked Moved C#
helpquestion
4 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.
  • Z Offline
    Z Offline
    zhoujun
    wrote on last edited by
    #1

    I learned that class consists of members(data member or member function); but in other articles I see new terms such as property and method. Does method can be compared to member function? And why use properties(get,set)? Thanks. this is my signature for forums quoted from shog*9: I can't help but feel, somewhere deep within that withered, bitter, scheming person, there is a small child, frightened, looking a way out.

    C I L 3 Replies Last reply
    0
    • Z zhoujun

      I learned that class consists of members(data member or member function); but in other articles I see new terms such as property and method. Does method can be compared to member function? And why use properties(get,set)? Thanks. this is my signature for forums quoted from shog*9: I can't help but feel, somewhere deep within that withered, bitter, scheming person, there is a small child, frightened, looking a way out.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      zhoujun wrote: And why use properties(get,set)? Because you generally in C++ make variables private and provide get/set methods so you can control access. With C#, you can do that, but use a syntax that is more like accessing variables. It's nice. zhoujun wrote: Does method can be compared to member function? Yes, they are the same. Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002 Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002 Again, you can screw up a C/C++ program just as easily as a VB program. OK, maybe not as easily, but it's certainly doable. - Jamie Nordmeyer - 15-Nov-2002

      1 Reply Last reply
      0
      • Z zhoujun

        I learned that class consists of members(data member or member function); but in other articles I see new terms such as property and method. Does method can be compared to member function? And why use properties(get,set)? Thanks. this is my signature for forums quoted from shog*9: I can't help but feel, somewhere deep within that withered, bitter, scheming person, there is a small child, frightened, looking a way out.

        I Offline
        I Offline
        ian mariano
        wrote on last edited by
        #3

        It's good object-oriented practice to use get/set for object properties, that way, you can change the underlying representation of the data without breaking the access method (get/set). The real underlying value [field] that's returned or set in get/set is either stored by the object instance, or retrieved from elsewhere, and by abstracting the get/set instead of directly accessing the underlying value allows you to change the representation without "breaking" your object clients. In C#, a property is a kind of method function, you just don't have to call it like a function ;)

        "The greatest danger to humanity is humanity without an open mind."
          - Ian Mariano - http://www.ian-space.com/

        1 Reply Last reply
        0
        • Z zhoujun

          I learned that class consists of members(data member or member function); but in other articles I see new terms such as property and method. Does method can be compared to member function? And why use properties(get,set)? Thanks. this is my signature for forums quoted from shog*9: I can't help but feel, somewhere deep within that withered, bitter, scheming person, there is a small child, frightened, looking a way out.

          L Offline
          L Offline
          LongRange Shooter
          wrote on last edited by
          #4

          Your library of 'functions' becomes your class. So a definition of class is a collections of methods that perform similar functions. The methods can get information passed to them using parameters or you can expose properties. Likewise you can have a method that you call which sets several values internally that you can retreive using properties. Properties is a means of exposing private information within your class to the public. An advantage of doing this is many fold: When a public consumer of your class puts data into a property you can hide complex calculations and processes that may be applied to that data. In a simple classroom example I show someone setting the number of gallons of beer the classroom is about to brew. Internally, I break that information out into the number of gallons, number of liters, computed weight by gallon in pounds, and computed weight in grams. All the user knows is that when they set gallons they can 'easily' find out how much they are making in liters. Another benefit is something called data hiding. If you expose your public properties using simple data types, you can shelter the user of your class from any changes in the actual data behind the scenes. Your get/set properties handle the converstion of the data from the complex data types into the simple data types without their knowledge. In general you can think of your class like a castle. Things go on inside of it that only those who are priveledged to be inside know what is happening. The properties are like small windows in the castle that let you see glimpses of whatever you are allowed to see within the castle. If you tell a courier at the door that troops must be sent to the north wall, you don't know everything that goes on to actually get those troops there. You just know it happens and your request is satisfied. In practice, a well managed class library can be developed and tested once. Each time it is reused you only have to test your USE of the class, not the implementation within the class. _____________________________________________ The world is a dangerous place.
          Not because of those that do evil,
              but because of those who look on and do nothing.

          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