Solved

Properties VS Data members

Posted on 2003-12-03
5
421 Views
Last Modified: 2010-04-16
Is it better to make all exposed things Properties as opposed to public data members?
0
Comment
Question by:daniel_bigham
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 9

Accepted Solution

by:
malharone earned 50 total points
ID: 9869823
it really depends on the application, and the usage of that class.

'-- vb syntax
case 1:
public cust as Customer
PRO: easy .. less typing
CON: you don't have any control over this object


case 2:
private cust_ as Customer
public property cust as customer
  get
     return cust_
  end get
  set (byval value as customer)
     cust_=value
  end set
end property
PRO: option for readonly/writeonly
CON: setting object is ONLY by value

case 3:
  private cust_ as customer
  public function getCustomer() as customer
       return cust_
  end function
  public function setCustomer(byref c_ as customer)
     cust_=c_
  end function
PRO: full control over get/set, and byref/byval
CON: too much typing
0
 
LVL 1

Author Comment

by:daniel_bigham
ID: 9869864
I understand these pros and cons.

Maybe I should be more specific. If you don't need any special control, is there any reason to use Properties over Public Members? It seems a bit inconsistent to have some of your members exposed as Properties and some as Public Members. For instance, the .NET framework seems to expose EVERYTHING as properties.
0
 
LVL 9

Expert Comment

by:malharone
ID: 9869893
what do you mean by "the .NET framework seems to expose EVERYTHING as properties."

it depends what class you're dealing with
becuase property statements and public variables are simply "properties", but visually they're not distinguished

but if you dont want any control, or you know that no one's is going to exploit the public members, then go head.
you also want to use "public" members, if you want the objects to be set by REF and not by value.
0
 
LVL 1

Author Comment

by:daniel_bigham
ID: 9900828
What I mean by the framework exposing everything as properties... if you examine the .NET class library, for instance the Form class, there are no public data members exposed directly. They are all exposed as properties. This motivated the question: If Microsoft never exposes public data members (always wraps them as properties) for their class library, is that evidence that it is best practice to always wrap public data members by properties?

Your answer seems to be "No", although I'm not sure you understood my motivation for asking the question (That MS seems to).
0
 
LVL 9

Expert Comment

by:malharone
ID: 9905234
not really ... it's just the editor (i think) is incapable of differentiating from an object being exposed directly through a "Public" declaration or through a public property declaration.
if you look at the .length "property" of an (any) array, you'll see it as listed only as a readonly property.
but there are some properties that must be declared using "public blahObject as BlahClass" as opposed to "public property blahObject as BlahClass ... get .. set"
Microsoft may have chosen not to use "public obj as class" way may be because it's not as safe as the set method of a property statement -- which forces param passing byval).
i've nt come across an example where it's not a propery statement, since MS lists class members in the a) Constructors; b) public properties; c) public methods; d) public events; e) protected properties; f) protected methods;

they should've broken properties into
i) public properties
ii) public data members
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question