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