-1

I have a Class named Person (Internal Class Person) and wanted to intiate on a winform.

internal class Person
    {
        public string Name { get; set; }
        public byte Age { get; set; }

       public Person(string name, byte age)
        {
            Name = name;
            Age = age;

        }


}

Let's say i want to create it within a class named "NewClass" so i would do the following:

  class NewClass
    {
        public Person John;
    
        Clase2()
        {
            
            this.John.Age = 33;
           
    
        }
    }

However whenever i try to do it on a Win form:

public partial class Form2 : Form
    {
        public Person Sam;

        public Form2()
        {
            InitializeComponent();
            //   Person test = new Person();
            this.Sam.Age = 33;

    }
}

I get the following message:

Error CS0052 Inconsistent accessibility: field type 'Person' is less accessible than field 'Form2.Sam' Prueba Clase Persona D:\Documents\Clases C#\Prueba\Prueba Clase Persona\Form2.cs 15 Active

Is that because System.Windows.Forms, is a reference outside my "app" ? Because whenever i make the class Person Public, instead of Internal, the debugger accepts it.

  • That's because you have declared your Form2 `public`, but your Person `internal`. A public class cannot declare public members of a type that is not also public. – PMF Jun 22 '21 at 18:45
  • Does this answer your question? [Inconsistent accessibility: field type 'world' is less accessible than field 'frmSplashScreen](https://stackoverflow.com/questions/12990309/inconsistent-accessibility-field-type-world-is-less-accessible-than-field-fr) – Alejandro Jun 22 '21 at 18:46
  • Hi Alejandro, i already stated that making Public class it would work. So the answer provided on the link example didn't answer my question. However Chris Tavares and Anders Forsgren made the point and answered correctly. – Lucas P. Leiva Fernández Jun 22 '21 at 19:00

2 Answers2

3

You are exposing your Person class publicly via the public field in your Form class. Public fields must have public types.

If your type is internal, all uses of it in must also be internal. That includes public fields, arguments, return types and so on.

Anders Forsgren
  • 10,827
  • 4
  • 40
  • 77
2

The error message you're getting pretty clearly states the problem:

Inconsistent accessibility: field type 'Person' is less accessible than field

Basically, the field Sam is public. The class Form2 is public. However the type Person is NOT public.

If you made the Sam field private or internal this would work. It has nothing to do with WinForms or with partial classes.

Chris Tavares
  • 29,165
  • 4
  • 46
  • 63