Cannot display the value of protected internal variable

mp3909

Well-known member
Joined
Apr 22, 2018
Messages
61
Location
UK
Programming Experience
3-5
I have this code in Assembly1:

C#:
using System;
using sample;

namespace ConsoleApp1
{
    class Test:Customers
    {
        public void Display()
        {
            Console.WriteLine(a);
        }
    }
    class Program
    {
        static void Main()
        {
            Test t = new Test();
            t.Display();
            Console.ReadLine();
        }
    }
}


Then in a different assembly (i.e. Assembly2) I have this code.

C#:
using System;
namespace sample
{
    public class Customers
    {
        protected internal int a = 10;
    }
    class Program
    {
        static void Main()
        {
        }
    }
}


Assembly1 has a reference to Assembly2.
When I run the code in Assembly1, the output is blank whereas I thought it would display the value of "a" which is 10.
Why is it not working?
 
Yes but I am using "protected" internal so it should be accessible from assemblies outside of the assembly that contains the member
 
Ah, good point. I'm not sure that I have ever used both before and I was thinking that their behaviour was ANDed but it is indeed ORed.

I just created a solution with two projects - a Console App and a Class Library - and copied and pasted your code and it worked as expected for me, i.e. displayed 10 in the console window.
 
Oh, I was creating a solution with two projects - a Console App and a Console App - no Class Library.
Could that explain why it wan not working for me?
 
Quite possibly. It doesn't make sense to create two applications. Applications don't reference each other. Reusable code goes in a library. That library can then be referenced by another library or an application.
 

Latest posts

Back
Top Bottom