473,385 Members | 1,342 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,385 software developers and data experts.

Declaring readonly public property in C#

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
Nov 18 '05 #1
4 1154
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" <jo****@surfeu.de.no.spam> wrote in message
news:uL**************@TK2MSFTNGP11.phx.gbl...
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

Nov 18 '05 #2
Aha, different from VB.NET yet again....

Thanks Martin!

JON
Nov 18 '05 #3
Jon Maz <jo****@surfeu.de.no.spam> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 18 '05 #4
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
Nov 18 '05 #5

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

Similar topics

4
by: Jon Maz | last post by:
Hi, Quick & easy one - why does this code give an error? ===== CODE ===== private int numberOfPages;
4
by: Moe Sizlak | last post by:
Hi There, I am trying to use the to obtain the information from 2 listmenus, if the user makes a selection the page is submitted and then based on the value selected is transfererd to another...
14
by: Codemonkey | last post by:
Just a silly question, but why can't you overload a writeonly property with a readonly one? Surely the compiler can tell the difference of which one to call at compile time, depending on if it is...
0
by: Brian Young | last post by:
Hi all. I'm using the Property Grid control in a control to manage a windows service we have developed here. The windows service runs a set of other jobs that need to be managed. The control...
2
by: Marcin Floryan | last post by:
I am creating a custom control (Inherits UserControl) and my control containt a TextBox control. TextBox control has a Property called "ReadOnly". I would like to expose this property outside my...
7
by: Iain Mcleod | last post by:
Hi This must be an often encountered problem. I want to declare an abstract class or an interface with nothing but several static constants so that I can use polymorphism when I call each of them...
2
by: miben | last post by:
I need to set a variable returned by a readonly property in a class by another class. So the only way to set that value is from a specific class and function. Public Sub Main Dim setter As New...
4
by: Opa Vito | last post by:
Hello, I can't find out if a DataSet (and/or datatable) can be declared public so the xml data can be read to the DataSet in the form load, and then can be acessed from whatever sub or function...
0
by: Tom Dacon | last post by:
Oops, what I gave you in my first response was the usual way that you use varying scope: Private _FieldValue As <some type> Public Property Something As <some type> Get Return _FieldValue...
4
by: Fred Blair | last post by:
Where is the correct place to declare an array so that it will be available anywhere in the project. I have been playing with one form that has two buttons and one listbox. I want each button...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.