I can understand why properties are neat if you want to limit access
(only get, no set), or you want to do some bookkeeping or sanity
checking on values (in set) or if you want to change the underlying
type without changing the property type etc.
But what is the use for properties like:
private int data;
public int Data
{
get
{
return data;
}
set
{
data = value;
}
}
?
I see this a lot. For all intents and purposes, you might just have a
public member instead. Even if you wanted to change the public member
later to a property, your interface would still be maintained. No
possibilities lost.
/David 19 1458
On Oct 3, 10:28 am, "pinkfloydho... @gmail.com"
<pinkfloydho... @gmail.comwrote :
<snip>
I see this a lot. For all intents and purposes, you might just have a
public member instead. Even if you wanted to change the public member
later to a property, your interface would still be maintained. No
possibilities lost.
No, the interface isn't maintained. There are various things you can
do with fields (such as passing them by reference) which you can't do
with properties. Likewise there are things which work with properties
(such as databinding) which don't work with fields. Anything using
reflection or serialization is likely to break as well.
It's best to use properties right from the start - I don't like having
any fields which aren't private. I see that it's a slight pain to have
the whole property code, but C# 3 will make this simpler:
public string Foo { get; private set; }
for instance.
Jon
On Oct 3, 5:28 am, "pinkfloydho... @gmail.com"
<pinkfloydho... @gmail.comwrote :
I can understand why properties are neat if you want to limit access
(only get, no set), or you want to do some bookkeeping or sanity
checking on values (in set) or if you want to change the underlying
type without changing the property type etc.
But what is the use for properties like:
private int data;
public int Data
{
get
{
return data;
}
set
{
data = value;
}
}
?
I see this a lot. For all intents and purposes, you might just have a
public member instead. Even if you wanted to change the public member
later to a property, your interface would still be maintained. No
possibilities lost.
/David
Hi David,
In addition to what Jon said, I also find it handy to have the
property accessors as a central place to set a breakpoint (or add log
line) to help in debugging.
John
As an addition to what other people have said, consider that your
current needs may also not be your future needs. Are you really going to
want to go through everything and change your variable use to property
use if you decide you do need some kind of sanity checking/other things
going on whenever that property is assigned? Personally, I think it
helps a bit with future-proofing as well.
Chris.
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote
No, the interface isn't maintained. There are various things you can
do with fields (such as passing them by reference) which you can't do
with properties. Likewise there are things which work with properties
(such as databinding) which don't work with fields. Anything using
reflection or serialization is likely to break as well.
I never quite thought of that, but you're completly right.
I'll remember these reasons, as I see this come up quite often during code
reviews. I kick code back with "Make these properties" and a debate then
begins.
--
Chris Mullins
"John Duval" <Jo********@gma il.comwrote in message:
In addition to what Jon said, I also find it handy to have the
property accessors as a central place to set a breakpoint (or add log
line) to help in debugging.
Just to be nitpicky (sorry, I can't help myself!), you can do both of those
with a variable as well.
Set a breakpoint on the variable, right click on the little red circle, and
select "When hit". Up will come a dialog box that lets you do all sorts of
fun. It's quite powerful, and is a big aid to debugging (as are all the
'Advanced' breakpoint functions).
Of course, you can do all this with a property accessor too, but even then I
often end up setting the breakpoint on the variable itself, in case
something unforseen is going on...
--
Chris Mullins
On Oct 4, 10:57 am, this.final...@g mail.com wrote:
You mean apart from proper encapsulation? Variable names are an
implementation detail, IMO.
For a read-write property the underlying field is exposed to the
outside and there isn't any encapsulation to care about.
No - the field's *value* is exposed, but not the field itself. I can
rename the field to my heart's content - it's an implementation
detail.
One might as
well make the field public. This gives clearer, crisper and easier to
maintain code. The rule "Never use public fields" isn't proper
encapsulation. Proper encapsulation is to hide fields that are not
needed from the outside, but also to realize that once in a while some
internal data of an object must be exposed and public fields are a
good candidate to implement this behavior.
Only if you deem the fact that it's directly stored as a field without
even the *potential* for validation or other storage as part of the
specification. I think that locks things down too much, personally.
I believe that a field is too low-level a concept to be part of the
exposed API for a well-designed component.
Because DaInterface has specified a property, and it isn't being
implemented in the ImplementerClas s. Yes, C# *could* generate a
property instead, but that would give two very different syntaxes for
properties.
If some property should be a simple read-write property doing nothing
but copying the value, then why not just implement it as a public
field? One might speculate that maybe in the future the distinction
between field and property will disappear, "field" would just be
shorthand for a non-restricted property with no method body.
I think that's very unlikely, seeing as they're logically different
things. A field is a storage location; a property is way of accessing
data. Changing between the two changes behaviour in various ways -
code using a read/write field may not compile when using a property
(think "pass by reference"), reflection will see different things,
etc.
In C# 3 life will be much simpler though:
public int Something { get; set; }
Ok, that looks neat. But, still, I would read that as: Something is
like a public field. And I would then erase the curly braces thing,
because it's redudant.
But it's *not* a public field. There's a different in the metadata,
there are different semantics in various ways. Why try to pretend it's
something it's not?
Jon
On Oct 3, 1:03 pm, "Chris Mullins [MVP - C#]" <cmull...@yahoo .com>
wrote:
"John Duval" <JohnMDu...@gma il.comwrote in message:
In addition to what Jon said, I also find it handy to have the
property accessors as a central place to set a breakpoint (or add log
line) to help in debugging.
Just to be nitpicky (sorry, I can't help myself!), you can do both of those
with a variable as well.
Set a breakpoint on the variable, right click on the little red circle, and
select "When hit". Up will come a dialog box that lets you do all sorts of
fun. It's quite powerful, and is a big aid to debugging (as are all the
'Advanced' breakpoint functions).
Of course, you can do all this with a property accessor too, but even then I
often end up setting the breakpoint on the variable itself, in case
something unforseen is going on...
--
Chris Mullins
Hi Chris,
Not sure I follow what you mean. If you have two classes like this:
public class TestClass1
{
public string TestValue; // breakpoint can NOT be set
}
public class TestClass2
{
private string _testValue;
public string TestValue { set { _testValue = value; } } //
breakpoint can be set
}
You can set a breakpoint whenever TestValue is set on TestClass2, but
you can't do it for TestClass1. Are we talking about the same thing?
And I agree about the "When Hit..." breakpoint options, they're very
useful.
John
"John Duval" <Jo********@gma il.comwrote in message
news:11******** **************@ g4g2000hsf.goog legroups.com...
On Oct 3, 1:03 pm, "Chris Mullins [MVP - C#]" <cmull...@yahoo .com>
wrote:
>"John Duval" <JohnMDu...@gma il.comwrote in message:
In addition to what Jon said, I also find it handy to have the
property accessors as a central place to set a breakpoint (or add log
line) to help in debugging.
Just to be nitpicky (sorry, I can't help myself!), you can do both of those with a variable as well.
Set a breakpoint on the variable, right click on the little red circle, and select "When hit". Up will come a dialog box that lets you do all sorts of fun. It's quite powerful, and is a big aid to debugging (as are all the 'Advanced' breakpoint functions).
Of course, you can do all this with a property accessor too, but even then I often end up setting the breakpoint on the variable itself, in case something unforseen is going on...
-- Chris Mullins
Hi Chris,
Not sure I follow what you mean. If you have two classes like this:
public class TestClass1
{
public string TestValue; // breakpoint can NOT be set
}
public class TestClass2
{
private string _testValue;
public string TestValue { set { _testValue = value; } } //
breakpoint can be set
}
You can set a breakpoint whenever TestValue is set on TestClass2, but
you can't do it for TestClass1. Are we talking about the same thing?
And I agree about the "When Hit..." breakpoint options, they're very
useful.
John
Also, data breakpoints only work in native code, because if the garbage
collector moves the object... MS needs to get their debugging act together
and make the GC update breakpoint registers pointing into the managed heap.
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:11******** *************@y 42g2000hsy.goog legroups.com...
On Oct 4, 10:57 am, this.final...@g mail.com wrote:
You mean apart from proper encapsulation? Variable names are an
implementation detail, IMO.
For a read-write property the underlying field is exposed to the outside and there isn't any encapsulation to care about.
No - the field's *value* is exposed, but not the field itself. I can
rename the field to my heart's content - it's an implementation
detail.
You cannot rename the public member. Fields and properties are alike in
that regard.
>
>One might as well make the field public. This gives clearer, crisper and easier to maintain code. The rule "Never use public fields" isn't proper encapsulatio n. Proper encapsulation is to hide fields that are not needed from the outside, but also to realize that once in a while some internal data of an object must be exposed and public fields are a good candidate to implement this behavior.
Only if you deem the fact that it's directly stored as a field without
even the *potential* for validation or other storage as part of the
specification. I think that locks things down too much, personally.
I believe that a field is too low-level a concept to be part of the
exposed API for a well-designed component.
Even for something like System.Drawing. Point?
What about interop structures? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: John Baro |
last post by:
I have a class as:
public class a :
{
private float m_b;
public float b
{
get
{
|
by: Bill O |
last post by:
We have some classes with simple properties:
public class StringClass
{
|
by: James Allen Bressem |
last post by:
If you aare going to change the entire Basic language in a new distribution
the least Microsoft coulds have done for VBers is give us a new simplified
syntax. I have seen many cases in VB.net where they seem to have had "no
insightfullness" into these considerations.
Their implementation of the property for example is gaudy at best:
Property Name(ByVal Value As String) As String
Get
|
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....
|
by: Jon Slaughter |
last post by:
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
In the above, why doesn't C# just allow one to create a single directive to
make a property?
| |
by: Paul Mcilreavy |
last post by:
Hi, i would like to get some view points on the use of properties within
classes. My belief is that properties should generally be used to return
values of private members. They should not do anything that is likely to
return an error and they should not instantiate or return any big objects
etc.
My company seems to be using them for everything which is starting to cause
me concern. Any comments or other viewpoints is appreciated.
|
by: miskomisko |
last post by:
..net v2.0, VS 2005, VB6
Hi,
I try to import my vb6 application to vb .net. The application uses my
special activeX component which i want to import. When i import ocx
file. Component is imported but not with all its properties. The
property:
Public Property Get Value(ByVal iRow As Long, ByVal iCol As Long) As
|
by: Dunc |
last post by:
Has anyone come across a way to bind simple properties (e.g. <%#
DateTime.Now.Year %>, <%# UserName %in the underlying HTML) without
doing a general DataBind(); call?
Not only does it seem a massive waste of resources, I find this call
extremely annoying, particularly when working with Master pages where
inevitably I'll end up wiping out any manually build controls such as
added options to dropdowns.
It would be great if there was a...
|
by: McMurphy |
last post by:
I have a single table which I would like to search on a unique column
varchar(15) that may have some nulls. Employee social club member no,
some employees have a number and others don't. Those that do have a number
will all have a unique number.
I had added an index using:
ALTER TABLE employees ADD INDEX(emp_socialclubno);
However when I run:
mysqlexplain select employeeid from employees where
|
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...
|
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,...
| |
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |