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

Simplified property syntax?

WXS
Is there or should there be a simplified property syntax?

For example we have to write:
class MyClass {
private int _field;
public int Field { get { return _field;} set { _field=value}}
}

What if instead you could write:

class MyClass {
public property int Field;
}

Is something like this syntax available? If not should it be? It seems this
is much cleaner and prevents forcing writing a backing store for each
property seperately.

The benefit of this is cleaner coding for properties that just use a backing
field, plus compatibility to later change the implementation for that field
if needed without breaking the interface.
Mar 14 '06 #1
13 1788
> Is there or should there be a simplified property syntax?

For example we have to write:
class MyClass {
private int _field;
public int Field { get { return _field;} set { _field=value}}
}
What if instead you could write:

class MyClass {
public property int Field;
}
Is something like this syntax available? If not should it be? It
seems this is much cleaner and prevents forcing writing a backing
store for each property seperately.

The benefit of this is cleaner coding for properties that just use a
backing field, plus compatibility to later change the implementation
for that field if needed without breaking the interface.


I'm sure I'm missing something obvious here but wouldn't the only difference
between:

public int Field;

and the hypothetical:

public property int Field;

be that Reflection would tag one as a field and the other as a property?
Would there actually be anything you could change with this and thus "change
the interface"?

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Mar 14 '06 #2

"Lasse Vågsæther Karlsen" <la***@vkarlsen.no> wrote in message
news:a0*************************@news.microsoft.co m...
Is there or should there be a simplified property syntax?

For example we have to write:
class MyClass {
private int _field;
public int Field { get { return _field;} set { _field=value}}
}
What if instead you could write:

class MyClass {
public property int Field;
}
Is something like this syntax available? If not should it be? It
seems this is much cleaner and prevents forcing writing a backing
store for each property seperately.

The benefit of this is cleaner coding for properties that just use a
backing field, plus compatibility to later change the implementation
for that field if needed without breaking the interface.


I'm sure I'm missing something obvious here but wouldn't the only
difference between:

public int Field;

and the hypothetical:

public property int Field;

be that Reflection would tag one as a field and the other as a property?
Would there actually be anything you could change with this and thus
"change the interface"?


Properties are implemented as methods (set_Field and get_Field in this
case). The compiler would need to create these and the underlying field.

Personaly I can live with the extra typing.
Mar 14 '06 #3
Hi,

And what exactly is the difference between your proposed feature and making
your variable public?

"WXS" <WX*@discussions.microsoft.com> wrote in message
news:68**********************************@microsof t.com...
Is there or should there be a simplified property syntax?

For example we have to write:
class MyClass {
private int _field;
public int Field { get { return _field;} set { _field=value}}
}

What if instead you could write:

class MyClass {
public property int Field;
}

Is something like this syntax available? If not should it be? It seems
this
is much cleaner and prevents forcing writing a backing store for each
property seperately.

The benefit of this is cleaner coding for properties that just use a
backing
field, plus compatibility to later change the implementation for that
field
if needed without breaking the interface.

Mar 14 '06 #4
WXS
When you have a lot of properties that is a lot of extra code and typing, and
unless using a tool you could even cut and paste in the wrong backing store
for a property. It just seems something like this would be cleaner since it
is a fairly common idiom to have a backing store variable.

"Nick Hounsome" wrote:

"Lasse Vågsæther Karlsen" <la***@vkarlsen.no> wrote in message
news:a0*************************@news.microsoft.co m...
Is there or should there be a simplified property syntax?

For example we have to write:
class MyClass {
private int _field;
public int Field { get { return _field;} set { _field=value}}
}
What if instead you could write:

class MyClass {
public property int Field;
}
Is something like this syntax available? If not should it be? It
seems this is much cleaner and prevents forcing writing a backing
store for each property seperately.

The benefit of this is cleaner coding for properties that just use a
backing field, plus compatibility to later change the implementation
for that field if needed without breaking the interface.


I'm sure I'm missing something obvious here but wouldn't the only
difference between:

public int Field;

and the hypothetical:

public property int Field;

be that Reflection would tag one as a field and the other as a property?
Would there actually be anything you could change with this and thus
"change the interface"?


Properties are implemented as methods (set_Field and get_Field in this
case). The compiler would need to create these and the underlying field.

Personaly I can live with the extra typing.

Mar 14 '06 #5
WXS
If you make your variable public you don't have the option later to change
your implementation, since the compiler/CLR would allow direct access to the
field. By saying it is a property it will actual compile to get_ set_
methods so that later if you wanted to make a change where a simple backing
store variable no longer was viable you could without breaking existing code
that used your class.

"Ignacio Machin ( .NET/ C# MVP )" wrote:
Hi,

And what exactly is the difference between your proposed feature and making
your variable public?

"WXS" <WX*@discussions.microsoft.com> wrote in message
news:68**********************************@microsof t.com...
Is there or should there be a simplified property syntax?

For example we have to write:
class MyClass {
private int _field;
public int Field { get { return _field;} set { _field=value}}
}

What if instead you could write:

class MyClass {
public property int Field;
}

Is something like this syntax available? If not should it be? It seems
this
is much cleaner and prevents forcing writing a backing store for each
property seperately.

The benefit of this is cleaner coding for properties that just use a
backing
field, plus compatibility to later change the implementation for that
field
if needed without breaking the interface.


Mar 14 '06 #6
You have this option in C++/CLI.
It's very handy for cases where the get and set are both single lines. You
aren't the only one who finds typing 11 lines for a 'dumb' property annoying
(counting the braces...).
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter

"WXS" wrote:
Is there or should there be a simplified property syntax?

For example we have to write:
class MyClass {
private int _field;
public int Field { get { return _field;} set { _field=value}}
}

What if instead you could write:

class MyClass {
public property int Field;
}

Is something like this syntax available? If not should it be? It seems this
is much cleaner and prevents forcing writing a backing store for each
property seperately.

The benefit of this is cleaner coding for properties that just use a backing
field, plus compatibility to later change the implementation for that field
if needed without breaking the interface.

Mar 14 '06 #7
WXS
Thanks for the reminder on the C++/CLI-- I thought I remembered something
like this but couldn't find it on the C# side... forgot it was C++/CLI.
Certainly C# could use this.
"David Anton" wrote:
You have this option in C++/CLI.
It's very handy for cases where the get and set are both single lines. You
aren't the only one who finds typing 11 lines for a 'dumb' property annoying
(counting the braces...).
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter

"WXS" wrote:
Is there or should there be a simplified property syntax?

For example we have to write:
class MyClass {
private int _field;
public int Field { get { return _field;} set { _field=value}}
}

What if instead you could write:

class MyClass {
public property int Field;
}

Is something like this syntax available? If not should it be? It seems this
is much cleaner and prevents forcing writing a backing store for each
property seperately.

The benefit of this is cleaner coding for properties that just use a backing
field, plus compatibility to later change the implementation for that field
if needed without breaking the interface.

Mar 14 '06 #8
It's even more of a relief in C++/CLI since normally you have to repeat the
return type in three places in a C++/CLI long-hand property - property
header, get, and the set (never could figure out the logic behind that).
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter

"WXS" wrote:
Thanks for the reminder on the C++/CLI-- I thought I remembered something
like this but couldn't find it on the C# side... forgot it was C++/CLI.
Certainly C# could use this.
"David Anton" wrote:
You have this option in C++/CLI.
It's very handy for cases where the get and set are both single lines. You
aren't the only one who finds typing 11 lines for a 'dumb' property annoying
(counting the braces...).
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter

"WXS" wrote:
Is there or should there be a simplified property syntax?

For example we have to write:
class MyClass {
private int _field;
public int Field { get { return _field;} set { _field=value}}
}

What if instead you could write:

class MyClass {
public property int Field;
}

Is something like this syntax available? If not should it be? It seems this
is much cleaner and prevents forcing writing a backing store for each
property seperately.

The benefit of this is cleaner coding for properties that just use a backing
field, plus compatibility to later change the implementation for that field
if needed without breaking the interface.

Mar 14 '06 #9

"WXS" <WX*@discussions.microsoft.com> wrote in message
news:AC**********************************@microsof t.com...
When you have a lot of properties that is a lot of extra code and typing,
and
unless using a tool you could even cut and paste in the wrong backing
store
for a property. It just seems something like this would be cleaner since
it
is a fairly common idiom to have a backing store variable.
If you have that many properties it is probably a poor OO design.

In a good OO design you ask objects to do things for you - you don't grab
all their data and do it yourself.

"Nick Hounsome" wrote:

"Lasse Vågsæther Karlsen" <la***@vkarlsen.no> wrote in message
news:a0*************************@news.microsoft.co m...
>> Is there or should there be a simplified property syntax?
>>
>> For example we have to write:
>> class MyClass {
>> private int _field;
>> public int Field { get { return _field;} set { _field=value}}
>> }
>> What if instead you could write:
>>
>> class MyClass {
>> public property int Field;
>> }
>> Is something like this syntax available? If not should it be? It
>> seems this is much cleaner and prevents forcing writing a backing
>> store for each property seperately.
>>
>> The benefit of this is cleaner coding for properties that just use a
>> backing field, plus compatibility to later change the implementation
>> for that field if needed without breaking the interface.
>>
>
> I'm sure I'm missing something obvious here but wouldn't the only
> difference between:
>
> public int Field;
>
> and the hypothetical:
>
> public property int Field;
>
> be that Reflection would tag one as a field and the other as a
> property?
> Would there actually be anything you could change with this and thus
> "change the interface"?


Properties are implemented as methods (set_Field and get_Field in this
case). The compiler would need to create these and the underlying field.

Personaly I can live with the extra typing.

Mar 14 '06 #10
WXS <WX*@discussions.microsoft.com> wrote:
Is there or should there be a simplified property syntax?


<snip>

Yes, I believe there should be, for the reasons you've given. In
addition, I'd like to be able to declare a field "inside" a property
declaration so that it's only available within that property (but has
the same lifetime as if it were declared outside). That would prevent
the field from accidentally being used within the class where the
property should be used instead. There are a few gotchas with this
approach, mostly involving serialization, but I think it would be
useful.

--
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
Mar 14 '06 #11
WXS
I'd agree with that as well. Is there an official suggestion submission
location we can send them to for MS?

"Jon Skeet [C# MVP]" wrote:
WXS <WX*@discussions.microsoft.com> wrote:
Is there or should there be a simplified property syntax?


<snip>

Yes, I believe there should be, for the reasons you've given. In
addition, I'd like to be able to declare a field "inside" a property
declaration so that it's only available within that property (but has
the same lifetime as if it were declared outside). That would prevent
the field from accidentally being used within the class where the
property should be used instead. There are a few gotchas with this
approach, mostly involving serialization, but I think it would be
useful.

--
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

Mar 14 '06 #12
WXS <WX*@discussions.microsoft.com> wrote:
I'd agree with that as well. Is there an official suggestion submission
location we can send them to for MS?


I submitted that one ages ago, although more voices wouldn't hurt.

You could make a full feature request on the MSDN product feedback
centre:

http://lab.msdn.microsoft.com/produc...k/default.aspx

--
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
Mar 14 '06 #13
WXS
Thanks I added one.

"Jon Skeet [C# MVP]" wrote:
WXS <WX*@discussions.microsoft.com> wrote:
I'd agree with that as well. Is there an official suggestion submission
location we can send them to for MS?


I submitted that one ages ago, although more voices wouldn't hurt.

You could make a full feature request on the MSDN product feedback
centre:

http://lab.msdn.microsoft.com/produc...k/default.aspx

--
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

Mar 14 '06 #14

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

Similar topics

3
by: Stephan Kurpjuweit | last post by:
Hi! I am looking for a simplified Docbook XML schema. As far as I know, "Simplified Docbook" only exists as DTD, right? Could you please send me pointers or ideas that might be useful for me? ...
6
by: Zhang Weiwu | last post by:
Hello. I am working with a php software project, in it (www.egroupware.org) Chinese simplified locate is "zh" while Traditional Chinese "tw". I wish to send correct language attribute in http...
5
by: Jon Maz | last post by:
Hi All, I'm reasonably proficient with C#, and am starting with php5. I was happy to learn of the __get and __set methods, but am encountering a problem using them to set an object property of...
5
by: Ahmad Jalil Qarshi | last post by:
i want to store binary data into my property. i read earlier positing on "microsoft.public.dotnet.languages.vc" group with subject as SAFEARRAY in attributed ATL7 Project. i followed the same...
3
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...
16
by: pamelafluente | last post by:
I am still working with no success on that client/server problem. I need your help. I will submit simplified versions of my problem so we can see clearly what is going on. My model: A client...
0
by: Memfis | last post by:
While I was looking through the group's archives I came across a discussion on doing properties (as known from Delphi/C#/etc) in C++. It inspired me to do some experimenting. Here's what I came up...
3
by: bearophileHUGS | last post by:
Probably you have already discussed this topic, but maybe you can stand touching it again briefly. Maybe properties aren't used so often to deserve a specific syntax, but I don't like their syntax...
4
by: innes | last post by:
Hi, I'm unable to work out the syntax for explicit implementation of an interface property. Example Code: ----- public interface class ITest { int M();
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
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?
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
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...
0
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...
0
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...

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.