473,394 Members | 1,865 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,394 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 15 '05 #1
4 16336
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 15 '05 #2
Aha, different from VB.NET yet again....

Thanks Martin!

JON
Nov 15 '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 15 '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 15 '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...
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...
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:
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.