473,386 Members | 1,810 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,386 software developers and data experts.

properties vs functions



Hi,

What is the difference between Properties and functions.
Why dont we go for functions instead of properties.
Regrads
*** Sent via Developersdex http://www.developersdex.com ***
Sep 12 '06 #1
11 6769
"Kondapanaidu" <kondapanaiduwrote in message
news:eH**************@TK2MSFTNGP04.phx.gbl...
>

Hi,

What is the difference between Properties and functions.
Why dont we go for functions instead of properties.
Properties is just a cleaner way of doing things. Instead of having, for
example, Start, Stop and IsRunning methods we can just have a Running
property that can be written to or read. A property usuaully indicates there
is little work involved although this is only a general rule.

Michael
Sep 12 '06 #2
"Kondapanaidu" <kondapanaiduwrote in message
news:eH**************@TK2MSFTNGP04.phx.gbl...
What is the difference between Properties and functions.
Define "functions". IMHO, both properties and methods (the labels that .NET
uses) are functions.
Why dont we go for functions instead of properties.
If you mean "why don't we go for methods instead of properties", IMHO it's
all about using the language to more clearly describe what something does.

To me, a property is a way to get or set some state of a class (oddly
enough, this fits very well with how one defines a property :) ). A method
is a part of a class that actually *does* something. Obviously, you could
use a method to get or set state. You could even use a property to actually
*do* something. But in the latter case, you would always have to return
something or accept a value. One of the things I'm finding is that, because
of the strong emphasis on exception handling in .NET, very few of my methods
ever actually return something and those that do are almost always returning
an object (often newly created).

So in a large majority of cases, another major differentiating factor is
that properties always have data going into or out of the class, while a
method does not return any specific value.

Yet another way to look at it is that properties are often a bi-directional
way to communicate to a class. That is, data can go either direction in a
symmetric way. That's not strictly required, of course, and I do have
plenty of read-only properties (I even made a write-only property once...I'm
still trying to figure out if I like that or not :) ). Methods may be
bidirectional in some sense, but you have to go to some trouble to make them
symmetrically so, and even if you do they won't have the clean,
easy-to-understand semantics of a property.

IMHO, whether to use a property or a method depends a LOT less on how much
work there is to do than the semantics of the actual function. I can well
imagine some state that requires a significant amount of work to change but
which would still more properly be implemented as a property, at the same
time that I can imagine a method that is relatively simple (maybe only a few
lines of code, even) and yet which would make no sense as a property at all.

Pete
Sep 12 '06 #3
What do u meant?
There is no difference, because the properties are the methods that are
generated automatically.

Properties give u high level to control your fields
What is the difference between Properties and functions.
--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche

Sep 12 '06 #4
Good question. First, properties are actually functions (methods), but a
property may have both a setter and a getter accessor method, which are both
accessed via the Property name. Also, property getters and setters do not
have parameters (except in VB, of course). So, why use a Property instead of
a function?

Well, a property is generally something you treat as if it were a "property"
or "characteristic" or "state" of a class, whereas a method or function is
generally treated as a process, something to do or execute. For example,
System.Windows.Forms.Control has a property called "Height" which is the
height (in pixels) of the Control, a characteristic of the Control. Compare
this to the method OnLoad(EventArgs), which defines a process, something the
Control executes.

Contrast properties with fields in a class. A field is similar to a
variable, but exists at class level. It is a piece of data. A property may
*expose* a field or some data, via its getter and setter accessor methods,
but the methods are processes rather than data. What is the purpose of this
indirection?

There are several purposes. A field can be read to or written from. By
omitting the setter or getter method, a property can be read-only or
write-only. A property may expose a characteristic which is calculated, such
as the Size property of a Control. The Size property is of type
"System.Drawing.Size" and it contains both the Width and Height of the
Control. Rather than storing this data twice, it can be stored as Height and
Width, and exposed as a Size via properties, such as the following:

private int _Height;
private int _Width;
public int Height { get { return _Height; } set { _Height = value; } }
public int Width { get { return _Width; } set { _Width = value; } }
public Size Size
{
get { return new Size(_Width, _Height); }
set
{
_Width = value.Width;
_Height = value.Height;
}
}

There are, as I mentioned, other reasons why properties might be used
instead of fields. The bisc rule of thumb might be expressed as "If it is a
process, expose it as a method; if it is state, expose it as a property".

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

What You Seek Is What You Get.

"Kondapanaidu" <kondapanaiduwrote in message
news:eH**************@TK2MSFTNGP04.phx.gbl...
>

Hi,

What is the difference between Properties and functions.
Why dont we go for functions instead of properties.
Regrads
*** Sent via Developersdex http://www.developersdex.com ***

Sep 12 '06 #5
Hi,

"Michael C" <no****@nospam.comwrote in message
news:uy**************@TK2MSFTNGP03.phx.gbl...
"Kondapanaidu" <kondapanaiduwrote in message
news:eH**************@TK2MSFTNGP04.phx.gbl...
>>

Hi,

What is the difference between Properties and functions.
Why dont we go for functions instead of properties.

Properties is just a cleaner way of doing things. Instead of having, for
example, Start, Stop and IsRunning methods we can just have a Running
property that can be written to or read.
Well if you check the framework those especific actions ( Start, Stop ) are
method, not properties.

The theory is that a property return/set part of the data of the instance
like Name, Address, Phone. Whether a method (or function) perform some
actions over the instance that can potentially change its status.

That's why IsRunning is a property , it only return an indication of the
status of the instance. And that is why Start and Stop are method as they
are indication of an action to be performed

--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Sep 12 '06 #6
Kondapanaidu wrote:
What is the difference between Properties and functions.

Why dont we go for functions instead of properties.
I assume that you mean properties and methods.

C# uses the syntax:

public class Foobar
{
private int v;
...
public int V
{
get
{
return v;
}
set
{
v = value;
}
}

and

o.V = o.V + 123;

Java uses the syntax:

public class Foobar {
private int v;
...
public int getV() {
return v;
}
public void setV(int v) {
this.v = c;
}
}

and

o.setV(o.getV() + 123);

Both achieves the goal of encapsulating the access
to the private field.

Which is good.

The problem with the Java solution is that to use
reflection you need to rely on the method names starting
with get/is/set and thos enot being used for anything else.

The problem with the C# version is that nothing prevents
a coder from doing something completely different in the
get and set code.

In both cases you depend on the programmer using
the language correctly.

And therefore I do not consider any of the solution
optimal.

But apparently the C# team liked the C# syntax. Or they
had to provide something VB6/COM compatible. I dont know.

And you should *always* use the properties way in C#
even if you think the feature should not have been
in C# in the first place, because whoever reads your
code later will expect it that way.

Arne
Sep 12 '06 #7
"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:SHFNg.37739$_q4.3852@dukeread09...
Kondapanaidu wrote:
>What is the difference between Properties and functions.

Why dont we go for functions instead of properties.

I assume that you mean properties and methods.

C# uses the syntax:

public class Foobar
{
private int v;
...
public int V
{
get
{
return v;
}
set
{
v = value;
}
}

and

o.V = o.V + 123;
even better
o.V += 123;
it uses the getters/setters under the covers, but otherwise it looks
just like a public variable.

>
Java uses the syntax:

public class Foobar {
private int v;
...
public int getV() {
return v;
}
public void setV(int v) {
this.v = c;
}
}

and

o.setV(o.getV() + 123);
Yup...Ick..I really wish Java had properties (and a few other C#
goodies)
>
Both achieves the goal of encapsulating the access
to the private field.

Which is good.

The problem with the Java solution is that to use
reflection you need to rely on the method names starting
with get/is/set and thos enot being used for anything else.

The problem with the C# version is that nothing prevents
a coder from doing something completely different in the
get and set code.
This applies equally to the java case.
I mean there is no reason that you can't do anything you want inside
your getters and setters...other than public ridicule and humiliation
that is. :^)

In both cases you depend on the programmer using
the language correctly.
Isn't that always the case
And therefore I do not consider any of the solution
optimal.
Not sure what you think would be preferable.
But apparently the C# team liked the C# syntax. Or they
had to provide something VB6/COM compatible. I dont know.

And you should *always* use the properties way in C#
even if you think the feature should not have been
in C# in the first place, because whoever reads your
code later will expect it that way.
Also properties are one of the best things since sliced bread.
Of course if they are used in a stupid fashion you still can get a bad
tasting sandwich

Bill
Sep 12 '06 #8
Arne Vajhøj <ar**@vajhoej.dkwrote:

<snip>
The problem with the C# version is that nothing prevents
a coder from doing something completely different in the
get and set code.
That's not a problem. That's an advantage. There's nothing to say that
a property should map directly onto a private field. In many cases it
certainly doesn't - consider things like DateTime.Now, for instance.

Likewise, properties can provide validation etc.

Now, you *could* write a C# property called Name which actually
returned an address, but then you could write getName() in Java and
return an address equally - I doubt we'll ever get a programming
language which is able to chastise programmers for writing function
members whose job doesn't match their name.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 12 '06 #9
Bill Butler wrote:
>The problem with the Java solution is that to use
reflection you need to rely on the method names starting
with get/is/set and thos enot being used for anything else.

The problem with the C# version is that nothing prevents
a coder from doing something completely different in the
get and set code.

This applies equally to the java case.
I mean there is no reason that you can't do anything you want inside
your getters and setters...other than public ridicule and humiliation
that is. :^)
My point.
>In both cases you depend on the programmer using
the language correctly.

Isn't that always the case
Not necessarily.
>And therefore I do not consider any of the solution
optimal.

Not sure what you think would be preferable.
Something that semantically limits what those
"whatevers" can do.

But I have not designed the super language yet.

Well - probably never will.
>But apparently the C# team liked the C# syntax. Or they
had to provide something VB6/COM compatible. I dont know.

And you should *always* use the properties way in C#
even if you think the feature should not have been
in C# in the first place, because whoever reads your
code later will expect it that way.

Also properties are one of the best things since sliced bread.
Of course if they are used in a stupid fashion you still can get a bad
tasting sandwich
To me it just adds extra syntax to the language without
really solving the problem.

But I guess that most people have things they like about
a language and things they do not like.

Arne

Sep 12 '06 #10
Jon Skeet [C# MVP] wrote:
Arne Vajhøj <ar**@vajhoej.dkwrote:
>The problem with the C# version is that nothing prevents
a coder from doing something completely different in the
get and set code.

That's not a problem. That's an advantage. There's nothing to say that
a property should map directly onto a private field. In many cases it
certainly doesn't - consider things like DateTime.Now, for instance.

Likewise, properties can provide validation etc.

Now, you *could* write a C# property called Name which actually
returned an address, but then you could write getName() in Java and
return an address equally
That is exactly my point.

It is still a problem. For whatever reasons new syntax was
added to the language, which did not deliver any new
semantics.

I don't like that.
- I doubt we'll ever get a programming
language which is able to chastise programmers for writing function
members whose job doesn't match their name.
In general no. For get/set functionality then maybe.

Arne
Sep 12 '06 #11
Arne Vajhøj <ar**@vajhoej.dkwrote:
Now, you *could* write a C# property called Name which actually
returned an address, but then you could write getName() in Java and
return an address equally
That is exactly my point.

It is still a problem. For whatever reasons new syntax was
added to the language, which did not deliver any new
semantics.

I don't like that.
Properties deliver increase readability. They're syntactic sugar, but
then so are a lot of features of C#. The one I love over Java is the
"using" statement - it does nothing you can't do with try/finally, but
it makes life a lot easier.
- I doubt we'll ever get a programming
language which is able to chastise programmers for writing function
members whose job doesn't match their name.
In general no. For get/set functionality then maybe.
Not without crippling what you're able to *genuinely* do within
properties.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 13 '06 #12

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

Similar topics

22
by: Generic Usenet Account | last post by:
A lot has been said in this newsgroup regarding the "evil" set/get accessor methods. Arthur Riel, (of Vanguard Training), in his class, "Heuristis for O-O Analysis & Design", says that there is...
8
by: Edward Diener | last post by:
Is it possible for a derived class to override a property and/or event of its base class ?
2
by: Lachlan Hunt | last post by:
Hi, In JavaScript 1.5, objects can use special getter and setter functions for properties. However, these only seem to be implemented in Gecko and, AFAICT, don't seem to be part of ECMAScript. ...
12
by: Perre Van Wilrijk | last post by:
Hi there, When I started using VB6, I used to write classes with properties and functions as following ... Private lngf1 As Long Private strf2 As String Public Property Get f1() As Long...
11
by: Brent Ritchie | last post by:
Hello all, I have been using C# in my programming class and I have grown quite fond of C# properties. Having a method act like a variable that I can control access to is really something. As...
21
by: VMI | last post by:
WHy are the get/set properties so useful? I know they're used so that I don't directly access a variable, but why is that so useful? What would the difference be between using it directly and using...
9
by: Cylix | last post by:
The following example are going to create an object to store the client Information, I would like to new the object to init all the properties by function: setProperty() Can I set the value to...
17
by: The Frog | last post by:
Hello everyone, I am working on an application that can build database objects in MS Access from text files. I suppose you could call it a backup and restore type routine. Accessing the...
7
by: jkaashoek | last post by:
What is the best mechanism for using custom application properties? Do these go into the php.ini file or somewhere else? And what functions are available to handle such properties?
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.