Thread: Get without Set

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485

    Get without Set

    Hallo,

    Im looking for the correct syntax for creating an attribute in c# with just a get accessor.
    Something along the line of this:
    Code:
    public int ID { get;}
    Is that possible or do I have to resolve to this in order to get just the Get accessor?
    Code:
    int m_id;
    public int ID
            {
                get { return m_id; }
            }
    Thanks

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Well, SOMETHING needs to be able to set the automatic property. Otherwise, what's the point of having it?

    What you can do is:
    Code:
    public int ID { get; private set; }
    That will allow other classes to get the property, but only allow the class the property is defined in to set it.
    If you understand what you're doing, you're not learning anything.

  3. #3
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    another way that i like

    Code:
    private int readOnlyValue;
    
    public int ReadOnlyValue
    {
        get
        {
             return readOnlyValue;
        }
    }
    
    private void setValue
    {
         readOnlyValue = 1;
    }
    Last edited by xddxogm3; 11-16-2010 at 05:30 PM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling errors and using Cmake Set Policy
    By eligor in forum Linux Programming
    Replies: 2
    Last Post: 01-25-2010, 01:43 PM
  2. Replies: 8
    Last Post: 01-18-2008, 04:06 AM
  3. The new FAQ
    By Hammer in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 08-30-2006, 10:05 AM
  4. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM
  5. Set default directory with GetTempDir?
    By Bajanine in forum Windows Programming
    Replies: 2
    Last Post: 05-04-2003, 11:36 PM