Cags Posted March 26, 2004 Posted March 26, 2004 If i have a value that I wish to be used within a custom control. And this value is never accessed from outside of that control, I assume its better to set and get it by assigning and calling the variables as opposed to writting a property... is this the case? Also if i wanted to access a property from outside the control, what are the advantages of using a property over a public variable. Quote Anybody looking for a graduate programmer (Midlands, England)?
Administrators PlausiblyDamp Posted March 26, 2004 Administrators Posted March 26, 2004 If it's never needed outside of the control (or class) then a variable is fine. Advantages of properties include - you can provide only a get or set (to make a property read or write only) as well as both. Property gets can include calculations or further processing to return the data in the correct form, property sets can perform validation etc - this removes the need for calling code to always have to provide their own validation logic. If the internal representation of the variable changes (i.e. double to decimal, dates being UTC based to regional based etc) then the property can still function correctly without calling code having to be updated - just the property code will need to be modified. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
TechnoTone Posted March 26, 2004 Posted March 26, 2004 Additionally, if the property influences other properties or modifies the look or behaviour of the control then you can put the code to manage this within the property set which is something you would not be able to do with a public variable. Quote TT (*_*) There are 10 types of people in this world; those that understand binary and those that don't.
Cags Posted March 29, 2004 Author Posted March 29, 2004 Thats pretty much what I thought, but its always good to get clarifications, thanks. Quote Anybody looking for a graduate programmer (Midlands, England)?
*Experts* Nerseus Posted March 29, 2004 *Experts* Posted March 29, 2004 Some "purists" say you should NEVER expose public fields (variables) - encapsulate everything in a property. I think that's generally a good idea but I have a few data-only classes that are just easier to code with public vars. I guess I'm no purist :) -nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
*Gurus* Derek Stone Posted March 29, 2004 *Gurus* Posted March 29, 2004 Twenty Hail-Mary's, Nerseus... twenty Hail-Mary's... Quote Posting Guidelines
TechnoTone Posted March 29, 2004 Posted March 29, 2004 TechnoTone flees before he gets found out by Derek Stone too. ;) Quote TT (*_*) There are 10 types of people in this world; those that understand binary and those that don't.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.