mariusxd
mariusxd

Reputation: 39

OOP c# Why do I get a protection level error when my item is public

public partial class Form1 : Form
{
    Elev x;
    ArrayList listaStudenti;
    ListViewItem itm;

    public Form1()
    {
        InitializeComponent();
        listaStudenti = new ArrayList();
        x=new Elev();         

    }
}

public class Elev
{
    public String nume;
    public int varsta;

    Elev(string _nume, int _varsta)
    {
        this.nume = _nume;
        this.varsta = _varsta;

    }
}

Error 1 'pregatiret.Elev.Elev(string, int)' is inaccessible due to its protection level c:\users\marius\documents\visual studio 2012\Projects\pregatiret\pregatiret\Form1.cs 26

Upvotes: 0

Views: 272

Answers (4)

RadioSpace
RadioSpace

Reputation: 952

Elev() needs to be public as well

this is a private constructor so it cannot be accessed

public Elev()...

Upvotes: 0

Habib
Habib

Reputation: 223402

Your class Elev's constructor doesn't have any access specifier. It is treated as private. Specify public access specifier.

public Elev(string _nume, int _varsta)
{
}

See: Access Modifiers (C# Programming Guide)

Class members, including nested classes and structs, can be public, protected internal, protected, internal, or private. The access level for class members and struct members, including nested classes and structs, is private by default.

You should consider using List<T> instead of ArrayList if you are targeting .Net framework 2.0 or higher.

Upvotes: 2

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101731

your constructor is private you should make it public

public Elev(string _nume, int _varsta)
{
    this.nume = _nume;
    this.varsta = _varsta;

}

Upvotes: 1

Ed Swangren
Ed Swangren

Reputation: 124790

Your constructor is not public, it's private:

Elev(string _nume, int _varsta)
{
    this.nume = _nume;
    this.varsta = _varsta;

}

The default access modifier in C# is private. You wanted:

public Elev(string _nume, int _varsta)
{
    this.nume = _nume;
    this.varsta = _varsta;

}

As an aside, there is no good reason to use ArrayList anymore unless you absolutely have to in order to interact with some old API.

Upvotes: 1

Related Questions