Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > Indigo

Vista - read only [DataMember]

 
 
Old 01-26-2007   #1 (permalink)
Dirk Reske


 
 

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 SpecsSystem Spec
Old 01-28-2007   #2 (permalink)
Arkady Frenkel


 
 

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 SpecsSystem Spec
Old 01-29-2007   #3 (permalink)
Marc Gravell


 
 

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 SpecsSystem Spec
Old 02-13-2007   #4 (permalink)
Keith Patrick


 
 

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

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46