Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How is it possible to access private members of a class becoz the property of the field is made protected internal ?

How is it possible to access private members of a class becoz the property of the field is made protected internal ?

Scheduled Pinned Locked Moved C#
comtutorialquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 3971400
    wrote on last edited by
    #1

    In the msdn page [^] it says "When a member of a class or struct is a property, field, method, event, or delegate, and that member either is a type or has a type as a parameter or return value, the accessibility of the member cannot be greater than the type. For example, you cannot have a public method M that returns a class C unless C is also public. Likewise, you cannot have a protected property of type A if A is declared as private. " But in this code

    class AccessSpecifier
    {
    // private field:
    private int wheels = 3;

       // protected internal property:
        protected internal int Wheels
        {
            get { return wheels; }
            set { wheels = value;  }
        }
    
    }
    class Derieved : AccessSpecifier
    {
        public void fun()
        {
            Wheels = 90; //accessing private mem of base class
        }
    }
    class main
    {
        public static void Main()
        {
            AccessSpecifier a = new AccessSpecifier();
            a.Wheels = 4;
            Console.WriteLine(a.Wheels); // gives 4 as output
            Derieved d = new Derieved();
            d.fun();
            Console.WriteLine(d.Wheels); //gives 90 as output
            d.Wheels = 99; //accessing private mem of a class in same assembly   :omg: 
            Console.WriteLine(d.Wheels);
            Console.ReadKey();
         
        }
    }
    

    How is this possible to access private members of a class jus becoz the property of the field is made protected internal ? And compiler is against the lines given in MSDN? Can u pls explain ?:confused:

    C V P 3 Replies Last reply
    0
    • U User 3971400

      In the msdn page [^] it says "When a member of a class or struct is a property, field, method, event, or delegate, and that member either is a type or has a type as a parameter or return value, the accessibility of the member cannot be greater than the type. For example, you cannot have a public method M that returns a class C unless C is also public. Likewise, you cannot have a protected property of type A if A is declared as private. " But in this code

      class AccessSpecifier
      {
      // private field:
      private int wheels = 3;

         // protected internal property:
          protected internal int Wheels
          {
              get { return wheels; }
              set { wheels = value;  }
          }
      
      }
      class Derieved : AccessSpecifier
      {
          public void fun()
          {
              Wheels = 90; //accessing private mem of base class
          }
      }
      class main
      {
          public static void Main()
          {
              AccessSpecifier a = new AccessSpecifier();
              a.Wheels = 4;
              Console.WriteLine(a.Wheels); // gives 4 as output
              Derieved d = new Derieved();
              d.fun();
              Console.WriteLine(d.Wheels); //gives 90 as output
              d.Wheels = 99; //accessing private mem of a class in same assembly   :omg: 
              Console.WriteLine(d.Wheels);
              Console.ReadKey();
           
          }
      }
      

      How is this possible to access private members of a class jus becoz the property of the field is made protected internal ? And compiler is against the lines given in MSDN? Can u pls explain ?:confused:

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      "only derived types or types within the same assembly can access that member." The key word is 'OR'. You are in the same assembly.

      Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp

      1 Reply Last reply
      0
      • U User 3971400

        In the msdn page [^] it says "When a member of a class or struct is a property, field, method, event, or delegate, and that member either is a type or has a type as a parameter or return value, the accessibility of the member cannot be greater than the type. For example, you cannot have a public method M that returns a class C unless C is also public. Likewise, you cannot have a protected property of type A if A is declared as private. " But in this code

        class AccessSpecifier
        {
        // private field:
        private int wheels = 3;

           // protected internal property:
            protected internal int Wheels
            {
                get { return wheels; }
                set { wheels = value;  }
            }
        
        }
        class Derieved : AccessSpecifier
        {
            public void fun()
            {
                Wheels = 90; //accessing private mem of base class
            }
        }
        class main
        {
            public static void Main()
            {
                AccessSpecifier a = new AccessSpecifier();
                a.Wheels = 4;
                Console.WriteLine(a.Wheels); // gives 4 as output
                Derieved d = new Derieved();
                d.fun();
                Console.WriteLine(d.Wheels); //gives 90 as output
                d.Wheels = 99; //accessing private mem of a class in same assembly   :omg: 
                Console.WriteLine(d.Wheels);
                Console.ReadKey();
             
            }
        }
        

        How is this possible to access private members of a class jus becoz the property of the field is made protected internal ? And compiler is against the lines given in MSDN? Can u pls explain ?:confused:

        V Offline
        V Offline
        Vikram A Punathambekar
        wrote on last edited by
        #3

        You are accessing the property Wheels which is protected, and not the private field wheels. Nothing odd with that; indeed, that is the reason you use properties.

        Cheers, Vikram.

        Recent activities: TV series: Friends, season 8 Books: Freakonomics, by Steven Levitt and Stephen J Dubner.


        Carpe Diem.

        C 1 Reply Last reply
        0
        • V Vikram A Punathambekar

          You are accessing the property Wheels which is protected, and not the private field wheels. Nothing odd with that; indeed, that is the reason you use properties.

          Cheers, Vikram.

          Recent activities: TV series: Friends, season 8 Books: Freakonomics, by Steven Levitt and Stephen J Dubner.


          Carpe Diem.

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          Actually, protected means he should not be able to see it. Protected internal, means he does, because it's internal.

          Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp

          V 1 Reply Last reply
          0
          • C Christian Graus

            Actually, protected means he should not be able to see it. Protected internal, means he does, because it's internal.

            Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp

            V Offline
            V Offline
            Vikram A Punathambekar
            wrote on last edited by
            #5

            I was talking about him using it in the Derived class. You are right in that internal alone would work, but so would protected alone.

            Cheers, Vikram.

            Recent activities: TV series: Friends, season 8 Books: Freakonomics, by Steven Levitt and Stephen J Dubner.


            Carpe Diem.

            1 Reply Last reply
            0
            • U User 3971400

              In the msdn page [^] it says "When a member of a class or struct is a property, field, method, event, or delegate, and that member either is a type or has a type as a parameter or return value, the accessibility of the member cannot be greater than the type. For example, you cannot have a public method M that returns a class C unless C is also public. Likewise, you cannot have a protected property of type A if A is declared as private. " But in this code

              class AccessSpecifier
              {
              // private field:
              private int wheels = 3;

                 // protected internal property:
                  protected internal int Wheels
                  {
                      get { return wheels; }
                      set { wheels = value;  }
                  }
              
              }
              class Derieved : AccessSpecifier
              {
                  public void fun()
                  {
                      Wheels = 90; //accessing private mem of base class
                  }
              }
              class main
              {
                  public static void Main()
                  {
                      AccessSpecifier a = new AccessSpecifier();
                      a.Wheels = 4;
                      Console.WriteLine(a.Wheels); // gives 4 as output
                      Derieved d = new Derieved();
                      d.fun();
                      Console.WriteLine(d.Wheels); //gives 90 as output
                      d.Wheels = 99; //accessing private mem of a class in same assembly   :omg: 
                      Console.WriteLine(d.Wheels);
                      Console.ReadKey();
                   
                  }
              }
              

              How is this possible to access private members of a class jus becoz the property of the field is made protected internal ? And compiler is against the lines given in MSDN? Can u pls explain ?:confused:

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              The statements you quote refer to the type of the class member. A property in class Y can't return a value of type X if type X is private to class Y.

              public class Y
              {
              private class X { ... }

              public X P { ... } // Not allowed
              

              }

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups