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.
Re: A question regarding nested class
Pl make class b public ..
Re: A question regarding nested class
Do paste the errors on Google search , many times you will
immediately get answer.
Re: A question regarding nested class
Quote:
Originally Posted by
LarryChen
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.
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
Re: A question regarding nested class
Quote:
Originally Posted by
Arjay
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.
Re: A question regarding nested class
Quote:
Originally Posted by
LarryChen
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
Re: A question regarding nested class
Quote:
Originally Posted by
Arjay
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.
Re: A question regarding nested class
Quote:
Originally Posted by
LarryChen
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?