-
June 28th, 2013, 05:12 PM
#1
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.
-
June 29th, 2013, 03:14 AM
#2
Re: A question regarding nested class
Pl make class b public ..
-
June 29th, 2013, 04:58 AM
#3
Re: A question regarding nested class
Do paste the errors on Google search , many times you will
immediately get answer.
-
June 30th, 2013, 01:51 AM
#4
Re: A question regarding nested class
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.
-
July 1st, 2013, 03:40 AM
#5
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
-
July 2nd, 2013, 03:09 PM
#6
Re: A question regarding nested class
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.
-
July 2nd, 2013, 07:02 PM
#7
Re: A question regarding nested class
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
-
July 3rd, 2013, 09:58 AM
#8
Re: A question regarding nested class
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.
-
July 3rd, 2013, 11:23 AM
#9
Re: A question regarding nested class
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?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|