0

I'm having trouble accessing my private class "inventory" in my class library, from my console application. I have referenced it and i can access "inventory" when its set to public class. Are there any ways to access it, when "inventory" is set to private?

using System;
using System.Collections.Generic;
using System.Text;


namespace InventoryManagementDND
{
    class Inventory
    {

The error feedback i get from visual studio is inaccessible due to its protection level.

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


namespace InventoryRunProgram
{
    class Program
    {            

        static void Main(string[] args)
        {
            int test = 0;
            Inventory I1 = new Inventory();

Solution explorer setup with the class library on top, console app on bottom

Lassek7
  • 9
  • 3
  • 1
    So why not make `Inventory` public ? – Sean Apr 06 '20 at 08:34
  • 4
    Note that it's not private - it's internal. And that's the whole point of things being internal - that you can't see them from other projects. This is a good thing, and you shouldn't try to work around it. If you want to be able to access it from your console app, make it public in your library project. – Jon Skeet Apr 06 '20 at 08:35
  • Why dou you want it to be private if you need it in another project. The purpose private access modifiere is to limit access by external code. – Ackdari Apr 06 '20 at 08:35
  • Do note the access modifier `internal` is intended to do that – Cleptus Apr 06 '20 at 08:35
  • 3
    there *is* a way to do what you are asking - `[assembly:InternalsVisibieTo(...)]` - however: that sounds like the wrong solution here; the *correct* solution would appear to be simply: make the type `public` – Marc Gravell Apr 06 '20 at 08:41
  • @MarcGravell that trick is really useful with unit tests of classes poorly developed – Cleptus Apr 06 '20 at 08:46

0 Answers0