[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Properties VS Data members

Posted on 2003-12-03
5
Medium Priority
?
424 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
  • 3
  • 2
5 Comments
 
LVL 9

Accepted Solution

by:
malharone earned 100 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month19 days, 15 hours left to enroll

873 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