Declaring readonly public property in C#

Discussion in 'ASP .Net' started by Jon Maz, Feb 20, 2004.

  1. Jon Maz

    Jon Maz Guest

    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
     
    Jon Maz, Feb 20, 2004
    #1
    1. Advertising

  2. Hi, Jon Maz,

    Please, read the first sentence in the following article:

    http://msdn.microsoft.com/library/en-us/csref/html/vclrfreadonlypg.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
    >
    >
     
    Martin Dechev, Feb 20, 2004
    #2
    1. Advertising

  3. Jon Maz

    Jon Maz Guest

    Aha, different from VB.NET yet again....

    Thanks Martin!

    JON
     
    Jon Maz, Feb 20, 2004
    #3
  4. 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
     
    Jon Skeet [C# MVP], Feb 20, 2004
    #4
  5. Jon Maz

    Jon Maz Guest

    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
     
    Jon Maz, Feb 20, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles A. Lackman
    Replies:
    1
    Views:
    1,373
    smith
    Dec 8, 2004
  2. SpamProof
    Replies:
    0
    Views:
    582
    SpamProof
    Oct 21, 2003
  3. Kevin Spencer
    Replies:
    2
    Views:
    3,314
    Kevin Spencer
    Sep 15, 2004
  4. Replies:
    3
    Views:
    520
    Damien
    Aug 8, 2006
  5. Jonathan Hyatt
    Replies:
    1
    Views:
    1,945
    Jonathan Hyatt
    Jun 8, 2004
Loading...

Share This Page