![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
| |
| | #1 (permalink) |
| | read only [DataMember] Hello, Is there a way to create a read only datamember? example [DataContract] public class User { [DataMember] public int Id; //<-- this should be readonly public User(int id) { this.Id = id; } } The client should know about it, but should not be able to change it. thanks Dirk |
My System Specs![]() |
| | #2 (permalink) |
| | Re: read only [DataMember] Do it private with only public getter ( without setter ) Arkady "Dirk Reske" <_freak.2k@gmx.net> wrote in message news:uFPbCiXQHHA.3812@TK2MSFTNGP06.phx.gbl... > Hello, > > Is there a way to create a read only datamember? > > example > > [DataContract] > public class User > { > [DataMember] > public int Id; //<-- this should be readonly > > public User(int id) > { > this.Id = id; > } > } > > The client should know about it, but should not be able to change it. > > thanks > Dirk |
My System Specs![]() |
| | #3 (permalink) |
| | Re: read only [DataMember] Generally the property is marked as [DataMember], however for this scenario you can mark the underlying field as [DataMember] (and perhaps rename in the attribute to keep you serialization the same). This allows the class to continue to expose the property as read-only, yet allow correct (de)serialization. Marc |
My System Specs![]() |
| | #4 (permalink) |
| | Re: read only [DataMember] Not to directly answer your question, but use of a public field (which has unrestricted access since it is just a publically-exposed chunk of memory) instead of a property is a discouraged practice (I believe FxCop will flag it). The ".Net way" to do what you're asking is: private int _Id; public int Id { get { return this._Id; } } |
My System Specs![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Read messages turn UN-read again | Live Mail | |||
| READ THIS IF YOU CANNOT GET DRIVE TO READ DISK TO FIX VISTA ERROR! | Vista hardware & devices | |||
| Folders/files read only/can't create new folder in read only folde | Vista account administration | |||