Akash
Akash

Reputation: 5012

Internal access modifier

I have a 2 classes in a project like this:

namespace ConsoleApplication1
{
    internal class ClassA
    {
         internal int dataMember;
    }
}

and

namespace ConsoleApplication1
{
    class ClassB 
    {
        static void Main(string[] args)
        {
            ClassA c = new ClassA();
            Console.Write(c.dataMember); //Cannot access??
        }
    }
}

I have used internal access modifier for the class A and its data member

Though class A's object can be created within the main of class b, but why I am not able to access its data member with the internal access specifier within the same assembly?

Here's the error that it gives in VS 2010:

'ConsoleApplication1.ClassA.dataMember' is inaccessible due to its protection level

Upvotes: 0

Views: 279

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1500055

You should be able to. Your exact code - just adding a using System; directive - compiles for me.

Check that you've actually saved all the files etc - and if there's a compile-time error, include that in your question. Perhaps there's something else wrong in ClassA (that you haven't shown us) which is preventing that class from compiling?

Also check that these really are in the same project - not just in the same namespace.

Upvotes: 2

Mahdi Tahsildari
Mahdi Tahsildari

Reputation: 13582

I'm getting results this way:

    internal class A
    {
        internal int X = 5;
    }
    static class B
    {
        public static void Show()
        {
            A a = new A();
            MessageBox.Show(a.X.ToString());
        }
    }
    private void Form2_Load(object sender, EventArgs e)
    {
        B.Show();
    }

and also this way:

    internal class A
    {
        internal int X = 5;
    }
    private void Form2_Load(object sender, EventArgs e)
    {
        A a = new A();
        MessageBox.Show(a.X.ToString());
    }

Upvotes: 0

Related Questions