Solved

Properties VS Data members

Posted on 2003-12-03
5
417 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 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now