none
Private members and Inherited class

    Question

  • Hello,

    Suppose we have Two class. A and B, B inherited from A and A have few private members. We don't have see and access of members in A class from B.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        class A
        {
            private int A_member { get; set; }
            public A(int a )
            {
                A_member = a;
                Console.WriteLine("A_Member");
            }
        }
    
        class B :A
        {
            public B(int b) :base(b) 
            {
                Console.WriteLine("B_Member");
            }
    
        }
        class Program
        {
            static void Main(string[] args)
            {
    
                B b = new B(100);
                b.A_member //Can't Access and See this private member
    
                Console.ReadKey();
            }
        }
    }

    Now in my project A class is FindForm and B class is FormReplace.

    Why we can see private members of FindForm in FormReplace? (True, they are locked but we can see private members).

    Thursday, February 1, 2018 6:17 AM

Answers

  • Hi,

    In fact, your question is strange in itself, you provided an example that A and B, so you get an result that you should not see the private members in the child form?

    No, you just can get a result that you can not access the private members in the child form:

            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form2());
    
                Form2 f2 = new Form2();
                f2.button1 //also you can not access the private member, this is your result
            }

    You can see the controls maybe because of the special UI design in winform.

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by KareninstructorMVP Friday, February 2, 2018 12:36 PM
    • Marked as answer by Arash_89 Wednesday, February 7, 2018 10:46 AM
    Thursday, February 1, 2018 7:04 AM
    Moderator

All replies

  • Hi,

    In fact, your question is strange in itself, you provided an example that A and B, so you get an result that you should not see the private members in the child form?

    No, you just can get a result that you can not access the private members in the child form:

            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form2());
    
                Form2 f2 = new Form2();
                f2.button1 //also you can not access the private member, this is your result
            }

    You can see the controls maybe because of the special UI design in winform.

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by KareninstructorMVP Friday, February 2, 2018 12:36 PM
    • Marked as answer by Arash_89 Wednesday, February 7, 2018 10:46 AM
    Thursday, February 1, 2018 7:04 AM
    Moderator
  • Hi Arash,

    Have you solved this problem now?

    If my reply provided you with a solution please mark the reply as answered as this will help others looking for the same or similar issues down the road.

    Best Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, February 7, 2018 9:15 AM
    Moderator