473,385 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Properties

I am a c++ programmer and quiet new to c sharp.
Unfortunately, i didnt understand what the adventage of a property is.

Let's assume, i have a class(Calculator) field:
int num;

Calculator c = new Calculator();
So, what for an advantage do i have if i write:
c.num = 5;
instead of :
c.Num = 5; // assuming that a property with this name and a set already
created

Thats almost the same, isn't it ?

--
Schwammkopf
Jan 26 '07 #1
3 2530
On Jan 26, 1:46 pm, "SpongeBob" <a...@sbox.tugraz.atwrote:
I am a c++ programmer and quiet new to c sharp.
Unfortunately, i didnt understand what the adventage of a property is.

Let's assume, i have a class(Calculator) field:
int num;

Calculator c = new Calculator();
So, what for an advantage do i have if i write:
c.num = 5;
instead of :
c.Num = 5; // assuming that a property with this name and a set already
created
If you will search these groups, you will find many discussions on this
issue. One reason for using a property is that you can validate the
data being put into the field. With direct field access, you cannot
control what will be assigned to that variable.

Jan 26 '07 #2
SpongeBob wrote:
I am a c++ programmer and quiet new to c sharp.
Unfortunately, i didnt understand what the adventage of a property is.
It permits changing the implementation after the fact.
Let's assume, i have a class(Calculator) field:
int num;

Calculator c = new Calculator();
So, what for an advantage do i have if i write:
c.num = 5;
instead of :
c.Num = 5; // assuming that a property with this name and a set already
created

Thats almost the same, isn't it ?
If you have clients (i.e. third-party code) that are compiled to use
(dynamically, like all .NET libraries) a library containing this class,
then they will have hard references to a field. If you later want to
update the library and calculate the value of the 'Num' field rather
than store it, or if you want to store the field somewhere else (maybe
in a hash table for 'sparse objects'), or if you want a read-only field,
then the fact that it's a field prevents you from doing these things.

Properties permit library authors to change the implementation of a
'field' after the fact. The reverse isn't true: once a field, always a
field.

-- Barry

--
http://barrkel.blogspot.com/
Jan 26 '07 #3
On Fri, 26 Jan 2007 20:46:08 +0100, "SpongeBob" <an**@sbox.tugraz.at>
wrote in <uT**************@TK2MSFTNGP05.phx.gbl>:
>I am a c++ programmer and quiet new to c sharp.
Unfortunately, i didnt understand what the adventage of a property is.
One thing that is important to understand is that the compiler creates
getters and setters for each property (assuming that both get and set
are specified). So, if you have a class with this property:

public string Foo
{
get { return m_foo; }
set { m_foo = value; }
}

The compiler will actually generate the methods get_Foo and set_Foo in
the assembly. The end result is very similar to creating your own
accessors in C++ instead of using a public member variable. The
advantage is that the syntax is nicer.

There are many advantages over public fields:

1. The get method can automatically calculate the value of the
property based on some internal data.

2. The set method can do all sorts of things, such as call member
methods to reevaluate the instance's state based on the value passed
in.

3. As others have pointed out, you can change the internal
implementation whenever you like without breaking the interface.

4. Using properties makes composition and delegation easy by allowing
you to wrap the composed class's properties in the composing class's,
just like with methods. You can't do this with public fields.

There are lots of other reasons. As Chris Dunaway suggested, check
the Google Groups archives for past discussions.
--
Charles Calvert | Software Design/Development
Celtic Wolf, Inc. | Project Management
http://www.celticwolf.com/ | Technical Writing
(703) 580-0210 | Research
Jan 30 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rick Austin | last post by:
I recently had to perform a reinstalltion of Windows XP (my registry seems to have become corrupt). After this completed I had to reinstall all applications since most use the registry for settings,...
4
by: Lyn | last post by:
Hi, This question may seem a bit academic... To learn more about Access VBA, I have been enumerating the properties of various form controls. This was mostly successful and I have learned a lot...
10
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
6
by: JerryP | last post by:
Hello, is there a way to launch the property dialogue for a directory from my c# app ? I would also like to launch the User Account Properties from Active Directory Users and Computers, and the...
3
by: Martin Montgomery | last post by:
I have, for example, a property called myProperty. I would like, when using a property grid to display the property name as "My Property". Is this possible. Is there an attribute etc Thank ...
7
by: Donald Grove | last post by:
Is it possible to retrieve field properties from a table in access2000 using code? I have tried: " dim dbs as dao.database dim tbl as dao.tabledef dim fld as dao.field dim prop as...
1
by: Christophe Peillet | last post by:
I have a CompositeControl with two types of properties: 1.) Mapped Properties that map directly to a child control's properties (ex.: this.TextboxText = m_txt.Text). These properties are handled...
7
by: Anderskj | last post by:
Hi! I am developing a c# application. I have a interface (which can change therefore my problem) If i do like this: List<PropertyInfoproperties = new List<PropertyInfo>();...
0
by: =?Utf-8?B?UmljayBHbG9z?= | last post by:
For some unknown reason (user error?), I cannot get a NameValueCollection to persist in the app.config file. Unlike other settings, I cannot get the String Collection Editor GUI to allow my to...
4
by: FullBandwidth | last post by:
I have been perusing various blogs and MSDN pages discussing the use of event properties and the EventHandlerList class. I don't believe there's anything special about the EventHandlerList class in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.