Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > Indigo

read only [DataMember]

 
 
Thread Tools Display Modes
Old 01-26-2007   #1 (permalink)
Dirk Reske
Guest


 

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


 

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



Old 01-29-2007   #3 (permalink)
Marc Gravell
Guest


 

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


Old 02-13-2007   #4 (permalink)
Keith Patrick
Guest


 

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



 

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
READ THIS IF YOU CANNOT GET DRIVE TO READ DISK TO FIX VISTA ERROR! reap70 Vista hardware & devices 1 02-10-2008 03:38 PM
Folders/files read only/can't create new folder in read only folde gshillitani Vista account administration 10 09-25-2007 09:52 AM
Read-Host issue, won't store to variable when using multiple read-host lines bryan.rutkowski@gmail.com PowerShell 7 02-09-2007 02:40 PM








Vistax64.com 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 2005-2008

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 47 48 49 50