473,804 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6807
"Kondapanai du" <kondapanaiduwr ote in message
news:eH******** ******@TK2MSFTN GP04.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
"Kondapanai du" <kondapanaiduwr ote in message
news:eH******** ******@TK2MSFTN GP04.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(EventArg s), 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.

"Kondapanai du" <kondapanaiduwr ote in message
news:eH******** ******@TK2MSFTN GP04.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******** ******@TK2MSFTN GP03.phx.gbl...
"Kondapanai du" <kondapanaiduwr ote in message
news:eH******** ******@TK2MSFTN GP04.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.d kwrote in message
news:SHFNg.3773 9$_q4.3852@duke read09...
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.d kwrote:

<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.co m>
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

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

Similar topics

22
12042
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 almost never an excuse for accessor methods. Personally, I do not go that far. I do feel that they serve a useful purpose (albeit in a limited manner). Personally I prefer dropping the "set" and "get" prefixes from the method names altogether. ...
8
2267
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
2778
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. Is there an alternative syntax I can use that is standardised in ECMAScript and also (preferably) interoperably implemented in several browsers? Or, do I have to use ordinary getFoo() and setFoo() functions.
12
1766
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 f1 = lngf1
11
2555
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 well as learning C#, I think that it's way overdue for me to start learning C++ Templates (I've been learning it for about 5 years now). I think that adding this type of functionality would be a good exercise to help learn template programming....
21
23992
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 get/set? Thanks.
9
1925
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 the object without using the global variable ? Thank you. ///---------------The object ------------------------------- function clientENV {
17
4037
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 fields, tables, relationships, and indexes is no issue for me via DAO code. The issue I have is that I am not sure which properties are actually necessary / available to set from code for each possible type of field. I have looked fo´r a reference on...
7
2210
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
9715
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10603
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10353
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10099
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7643
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4314
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3836
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.