473,325 Members | 2,872 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,325 software developers and data experts.

Properties intitialized in ctor

Why cannot properties be initialized in a constructor, especially read-
only onces.

public class {

pubic string Name {get, readonly set;}

}
Sep 29 '08 #1
6 1354
You can do this but not with automatic properties, you should just create
your own backing store and make it a readonly (just a get) properties which
returns a readonly variable.

--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com
"puzzlecracker" wrote:
Why cannot properties be initialized in a constructor, especially read-
only onces.

public class {

pubic string Name {get, readonly set;}

}
Sep 29 '08 #2
Typos aside, yes - it would be very nice to be able to declare
readonly auto-implemented properties, especially for writing immutable
classes. Presumably it would have to be a "private readonly" to make
sense. However, this simply isn't in the language at the moment.

You have two choices: for a true immutable, do it the long way:

private readonly string name;
public string Name {get {return name;}}
(and assign this.name in the ctor)

For a semi-immutable object, make the set private and simply don't set
it after the ctor:

public string Name {get; private set;}
(and assign this.Name in the ctor)

Marc
Sep 29 '08 #3
If it is readonly then it is a field and not a property....

public class Person
{
public readonly string Name;
}
Pete

Sep 29 '08 #4
In my opinion its still nice for encapsulation to have this as a property, to
shield the outside world from the source of the data. It could be possible in
future to change this to make Name = firstname + " " + lastname; and the
property would hide this from consumers.
--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com
"Peter Morris" wrote:
If it is readonly then it is a field and not a property....

public class Person
{
public readonly string Name;
}
Pete

Sep 29 '08 #5
In my opinion its still nice for encapsulation to have this as a property,
to
shield the outside world from the source of the data. It could be possible
in
future to change this to make Name = firstname + " " + lastname; and the
property would hide this from consumers.
At which point I would change it from a field to a property. I always use
fields for readonly because it saves typing, unless I need to use the class
as a datasource and therefore it must be a property, but then I usually need
a setter that does nothing just to get a column to show anyway.
Pete

Sep 29 '08 #6
The only issue with changing it when the needs change is that you would need
to rebuild the consumers even though you wont need to change their code.
Having it as a property from the start means you could just deploy a new
version of the assembly containing this code.

--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com
"Peter Morris" wrote:
In my opinion its still nice for encapsulation to have this as a property,
to
shield the outside world from the source of the data. It could be possible
in
future to change this to make Name = firstname + " " + lastname; and the
property would hide this from consumers.

At which point I would change it from a field to a property. I always use
fields for readonly because it saves typing, unless I need to use the class
as a datasource and therefore it must be a property, but then I usually need
a setter that does nothing just to get a column to show anyway.
Pete

Sep 29 '08 #7

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

Similar topics

1
by: Rich | last post by:
Hi, I have a query regarding VC6 and its handling of templated copy constructors. Here goes: Take a look at the following code sample... template<class _Ty, size_t t_uiSize = 10 > class...
10
by: richardclay09 | last post by:
The output of this: #include <iostream> #include <vector> using namespace std; struct X { int i; X(const X& x) : i(x.i) { cout << "ctor copy: " << i << endl;
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...
5
by: PasalicZaharije | last post by:
Hallo, few days ago I see ctor like this: Ctor() try : v1(0) { // some code } catch(...) { // some code }
8
by: Grizlyk | last post by:
Good morning. Look here: http://groups.google.com/group/fido7.ru.cpp.chainik/browse_frm/thread/7341aba5238c0f79 and here:...
5
by: peifeng_w | last post by:
Hi, try the following code with flag=0/1/2. #include<iostream> using namespace std; #define flag 2//option:0,1,2 class C {
3
by: John Salmon | last post by:
g++ complains about illegal access to a private member when the following is compiled with a private copy constructor for the class C. When the copy constructor is public, the program runs and...
7
by: Boki | last post by:
Hi All, I want to change WindowState of form1 from form2. I tried these two methods, but no luck on both. (1) Declare a public method: /* function of form1 */ public void...
2
by: subramanian100in | last post by:
If we do not provide any ctor for a class, the compiler provides the default ctor and copy ctor if needed. Consider a class Test. Suppose we provide some ctor in class Test but do not provide...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.