Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

Anybody looking for a graduate programmer (Midlands, England)?
  • Administrators
Posted

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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted
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.

TT

(*_*)

 

There are 10 types of people in this world;

those that understand binary and those that don't.

Posted
Thats pretty much what I thought, but its always good to get clarifications, thanks.
Anybody looking for a graduate programmer (Midlands, England)?
  • *Experts*
Posted

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

"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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...