CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2005
    Posts
    1,030

    A question regarding nested class

    Here is the code,
    Code:
    namespace ConsoleApplication1
    {
        class A
        {
            public A() 
            {
            }
    
            class B
            {
                public B()
                {
                    A a = new A();          
                }
            };
        };
    
        class Program
        {
            static void Main(string[] args)
            {
                A.B b = new A.B();
            }
        }
    }
    There is a compiler error "ConsoleApplication1.A.B' is inaccessible due to its protection level". Why? I thought by default, A.B is internal. So it should be able to be accessed by function Main.

  2. #2
    Join Date
    Jan 2012
    Location
    India
    Posts
    193

    Re: A question regarding nested class

    Pl make class b public ..

  3. #3
    Join Date
    Jan 2012
    Location
    India
    Posts
    193

    Cool Re: A question regarding nested class

    Do paste the errors on Google search , many times you will
    immediately get answer.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: A question regarding nested class

    Quote Originally Posted by LarryChen View Post
    There is a compiler error "ConsoleApplication1.A.B' is inaccessible due to its protection level". Why? I thought by default, A.B is internal. So it should be able to be accessed by function Main.
    I believe by default classes are private. If you are interested in this, why not write some code that uses reflection to tell you exactly what's going on? With reflection you can access all the attributes of the class including it's protection level.

  5. #5
    Join Date
    Jan 2012
    Location
    India
    Posts
    193

    Re: A question regarding nested class

    Arjay Sir , referring your answer , asking you a question. I had written a trial program , I designed
    own classes , prepared a library reference and did everything needed for that.

    But to use them in main and in other functions , I needed to make some classes and common variables public.
    that time , I felt , if I am using Public key word then what is use of property Data Abstraction / Hiding ....
    In case of MFC the things are completely different

  6. #6
    Join Date
    Jul 2005
    Posts
    1,030

    Re: A question regarding nested class

    Quote Originally Posted by Arjay View Post
    I believe by default classes are private. If you are interested in this, why not write some code that uses reflection to tell you exactly what's going on? With reflection you can access all the attributes of the class including it's protection level.
    By default classes are internal. Actually you can't define a class as private. Class must be either public or internal. But since class B is defined within class A, so class B might be private. I am kind of new to C#. Would you tell me how to use reflection to tell whether B is private or public? Thanks.

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: A question regarding nested class

    Quote Originally Posted by LarryChen View Post
    By default classes are internal. Actually you can't define a class as private. Class must be either public or internal. But since class B is defined within class A, so class B might be private. I am kind of new to C#. Would you tell me how to use reflection to tell whether B is private or public? Thanks.
    http://msdn.microsoft.com/en-us/library/493t6h7t.aspx

  8. #8
    Join Date
    Jul 2005
    Posts
    1,030

    Re: A question regarding nested class

    It looks like the argument BindingFlags of GetNestedTypes can only specify public or non-public. So I still can't tell if it is protected, private or internal? Thanks.

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: A question regarding nested class

    Quote Originally Posted by LarryChen View Post
    It looks like the argument BindingFlags of GetNestedTypes can only specify public or non-public. So I still can't tell if it is protected, private or internal? Thanks.
    Msdn is pretty helpful isn't it?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured