Xtreme .Net Talk
Forums
New posts
Search forums
What's new
New posts
New profile posts
Latest activity
Members
Registered members
Current visitors
New profile posts
Search profile posts
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Xtreme .Net Talk
.NET
General
Why shouldn't I use public variables inside a class?
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="PlausiblyDamp" data-source="post: 470937" data-attributes="member: 16331"><p>I take it you mean the idea of exposing variables as public rather than using properties...</p><p></p><p>The problem with public variables is the fact they can be modified from anywhere in the application with no restriction or validation whatsoever. If you declare the field as a property then you could perform validation in the <span style="font-family: 'Courier New'">set</span> part to ensure any values assigned are valid or simply omit the <span style="font-family: 'Courier New'">set </span>entirely to make the property read only and only allow changes through appropriate public methods. This effectively gives you more control over where changes to these variables can be made, allowing you to implement and validation or associated logic in one single place rather than multiple times throughout an application.</p></blockquote><p></p>
[QUOTE="PlausiblyDamp, post: 470937, member: 16331"] I take it you mean the idea of exposing variables as public rather than using properties... The problem with public variables is the fact they can be modified from anywhere in the application with no restriction or validation whatsoever. If you declare the field as a property then you could perform validation in the [FONT="Courier New"]set[/FONT] part to ensure any values assigned are valid or simply omit the [FONT="Courier New"]set [/FONT]entirely to make the property read only and only allow changes through appropriate public methods. This effectively gives you more control over where changes to these variables can be made, allowing you to implement and validation or associated logic in one single place rather than multiple times throughout an application. [/QUOTE]
Insert quotes…
Verification
Post reply
.NET
General
Why shouldn't I use public variables inside a class?
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.
Accept
Learn more…
Top
Bottom