locked
C#, static property

    Question

  • Can a static propery access instance variable? If so *how in the follwing case?

     

    using System;

    class MyClass
    {
        int  n; // *how?


        public static int property   
        {        
            get
            {
               return n;
            }
           
            set
            {   
                if(value>=0)
                    n = value;
            }
        }
    }

    class MainClass
    {
        static void Main()
        {
            Console.WriteLine("Initial value of property: {0} \n", MyClass.property);
                        
            MyClass.property = 100;
             
            Console.WriteLine("After assigning 100, value of property: {0} \n", MyClass.property);
           
            MyClass.property = -22;
              
            Console.WriteLine("After assigning -22, value of property: {0} \n", MyClass.property);       
        }
    }

    Monday, September 10, 2007 7:45 AM

Answers

  •  

    No it can't.

     

    Monday, September 10, 2007 8:04 AM
  • It can't.  In fact, the code you have will not build.  It'll come up with a "object reference is required for the nonstatic field" exception.

    try it  Smile

    Monday, September 10, 2007 8:37 AM
  • > Can a static propery access instance variable? If so *how in the follwing case?

    Surely, it can, but only through an instance of that class.

    Code Snippet

    class MyClass
    {
        int n;

        static readonly MyClass instance = new MyClass();


        public static int property
        {
            get
            {
                return instance.n;
            }

            set
            {
                if (value >= 0)
                    instance.n = value;
            }
        }
    }



    Tuesday, September 11, 2007 12:17 PM
  •  nikov wrote:
    > Can a static propery access instance variable? If so *how in the follwing case?

    Surely, it can, but only through an instance of that class.

    Code Snippet

    class MyClass
    {
        int n;

        static readonly MyClass instance = new MyClass();


        public static int property
        {
            get
            {
                return instance.n;
            }

            set
            {
                if (value >= 0)
                    instance.n = value;
            }
        }
    }


    This is just a trick.
    Here the static property is able to accessing a static field called instance which reference to a MyClass instance. (and the variable n is one of the member of that static instance)
    So, the complete answer should be "A static property can NOT access a non-static or external instance variable DIRECTLY" Smile
    Thanks
    Thursday, September 13, 2007 8:21 AM

All replies

  •  

    No it can't.

     

    Monday, September 10, 2007 8:04 AM
  • It can't.  In fact, the code you have will not build.  It'll come up with a "object reference is required for the nonstatic field" exception.

    try it  Smile

    Monday, September 10, 2007 8:37 AM
  • > Can a static propery access instance variable? If so *how in the follwing case?

    Surely, it can, but only through an instance of that class.

    Code Snippet

    class MyClass
    {
        int n;

        static readonly MyClass instance = new MyClass();


        public static int property
        {
            get
            {
                return instance.n;
            }

            set
            {
                if (value >= 0)
                    instance.n = value;
            }
        }
    }



    Tuesday, September 11, 2007 12:17 PM
  •  nikov wrote:
    > Can a static propery access instance variable? If so *how in the follwing case?

    Surely, it can, but only through an instance of that class.

    Code Snippet

    class MyClass
    {
        int n;

        static readonly MyClass instance = new MyClass();


        public static int property
        {
            get
            {
                return instance.n;
            }

            set
            {
                if (value >= 0)
                    instance.n = value;
            }
        }
    }


    This is just a trick.
    Here the static property is able to accessing a static field called instance which reference to a MyClass instance. (and the variable n is one of the member of that static instance)
    So, the complete answer should be "A static property can NOT access a non-static or external instance variable DIRECTLY" Smile
    Thanks
    Thursday, September 13, 2007 8:21 AM
  • Hey! That's fantastic. Thank you.

    Thursday, September 13, 2007 4:24 PM