In C#, How Can I Use Public Class Members(Methods Or Variables) From Different Assembly

2.3k views Asked by At

I am working on C# encapsulation from Tutorialspoint.com. And I read this

What is the difference between Public, Private, Protected, and Nothing?1
question from Stackoverflow. I read answer and i understood access specifiers in teoric. Now I want to make console application with this subject in visual studio.

public

The type or member can be accessed by any other code in the same assembly or another assembly that references it.

private

The type or member can only be accessed by code in the same class or struct.

protected

The type or member can only be accessed by code in the same class or struct, or in a derived class.

internal

The type or member can be accessed by any code in the same assembly, but not from another assembly.

protected internal

The type or member can be accessed by any code in the same assembly, or by any derived class in another assembly.

Variables or methods with public access specifier are accessed from same assembly and different assembly. But this station is different in internal description. Internal types variables and methods can accessed only same assembly but not different assembly in c#. I want to test this station in C#.So that i create two project and call method or variables between each other.

My Project Hierarchy

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TutorialsPoint.Encapsulation
{
   public class PublicEncapsulation
    {
        //member variables
        public double length;
        public double width;


        public double GetArea()
        {
            return length * width;
        }

        public void Display()
        {
            Console.WriteLine("Length: {0}", length);
            Console.WriteLine("Width: {0}", width);
            Console.WriteLine("Area: {0}", GetArea());
        }

    }
}

Above code is my 'PublicEncapsulation.cs' and i should call its members from other assembly.My other assembly project's class is Program.cs. I want to connect PublicEncapsulation.cs's members from Program.cs(other assembly). How can i do this calling operation from other assemblies in c#.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
using System.Collections;

namespace CallOtherAssemblyVariablesOrMethods
{
    class Program
    {
        static void Main(string[] args)
        {
            /*Call PublicEncapsulation.cs's members in there.*/
        }
    }
}

Above class is Program.cs. I want to call other asssembly PublicEncapsulation.cs's members in here.

2

There are 2 answers

0
Mert Özoğul On BEST ANSWER

2 days ago i have an simple problems. I solved my my problems with Stackoverflow. I wanted to see difference between internal and public access specifier. And then i created two project to see difference of them. If i could call public methods and couldn't call internal methods from other assembly and then theoretical knowledge is supported with C# console application. I wanted to do this. But i can't see other project's public members. And then i found solution in this How to use a Class from one C# project with another C# project tutorial. I should have added reference in project with right click.

SOLUTION STEPS

1. In the 'Solution Explorer' tree, expand the 'CallOtherAssemblyVariablesOrMethods' project and then right-click the project and select 'Add Reference' from the menu.

2. On the 'AddReference' dialog, select the 'Projects' tab and select your 'TutorialsPoint ' project.

3. If you are using namespaces then you will need to import the namespaces for your 'CallOtherAssemblyVariablesOrMethods' types by adding 'using' statements to your files in 'TutorialsPoint'.

Many thanks to everyone...

5
CodeCaster On

I guess in your Program.cs you have something like this:

var typeFromOtherAssembly = new InternalEncapsulation();

// Here you expect a compiler error:
var area = typeFromOtherAssembly.GetArea();

// This should return a string.
var details = typeFromOtherAssembly.Display();

You think the new and Display() would work, and that the (internal) GetArea() call would show a compiler error:

'InternalEncapsulation' does not contain a definition for 'GetArea' and no extension method 'GetArea' accepting a first argument of type 'InternalEncapsulation' could be found (are you missing a using directive or an assembly reference?)

But you didn't specify an access modifier for the InternalEncapsulation class, so it's internal:

Internal is the default if no access modifier is specified.

So at new InternalEncapsulation you get another compiler error:

InternalEncapsulation is inaccessible due to its protection level

So you need to make it public:

public class InternalEncapsulation