Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Declaring readonly public property in C#

Reply
Thread Tools

Declaring readonly public property in C#

 
 
Jon Maz
Guest
Posts: n/a
 
      02-20-2004
Hi,

Quick & easy one - why does this code give an error?


=====
CODE
=====

private int numberOfPages;

public readonly int NumberOfPages
{
get { return numberOfPages; }
}


======
ERROR
======

The modifier 'readonly' is not valid for this item (pointing to line
"public readonly int NumberOfPages").


TIA,

JON


 
Reply With Quote
 
 
 
 
Martin Dechev
Guest
Posts: n/a
 
      02-20-2004
Hi, Jon Maz,

Please, read the first sentence in the following article:

http://msdn.microsoft.com/library/en...readonlypg.asp

It answers your question.

Greetings
Martin
"Jon Maz" <> wrote in message
news:...
> Hi,
>
> Quick & easy one - why does this code give an error?
>
>
> =====
> CODE
> =====
>
> private int numberOfPages;
>
> public readonly int NumberOfPages
> {
> get { return numberOfPages; }
> }
>
>
> ======
> ERROR
> ======
>
> The modifier 'readonly' is not valid for this item (pointing to

line
> "public readonly int NumberOfPages").
>
>
> TIA,
>
> JON
>
>



 
Reply With Quote
 
 
 
 
Jon Maz
Guest
Posts: n/a
 
      02-20-2004
Aha, different from VB.NET yet again....

Thanks Martin!

JON


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      02-20-2004
Jon Maz <> wrote:
> Quick & easy one - why does this code give an error?


Because that's not how you make a property read-only. You make it read-
only by only giving it a get {...} accessor, not a set {...} accessor.

--
Jon Skeet - <>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Jon Maz
Guest
Posts: n/a
 
      02-20-2004
Hi Jon,

I thought of that, but it just looked kind of wrong (VB.Net habits, I
suppose), and the IDE gave me some kind of disapproving comment...

Still, I am now reassured.

Thanks!

JON


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd loyola MCSE 4 11-15-2006 02:40 AM
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd loyola Microsoft Certification 3 11-14-2006 05:18 PM
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd loyola MCSD 3 11-14-2006 05:18 PM
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd realexxams@yahoo.com Microsoft Certification 0 05-10-2006 02:35 PM
microsoft.public.dotnet.faqs,microsoft.public.dotnet.framework,microsoft.public.dotnet.framework.windowsforms,microsoft.public.dotnet.general,microsoft.public.dotnet.languages.vb Charles A. Lackman ASP .Net 1 12-08-2004 07:08 PM



Advertisments
 



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 51 52 53 54 55 56 57