problem wiith Class

cimerio

New member
Joined
Jul 11, 2023
Messages
4
Programming Experience
Beginner
Hello, guys.
I am not familiar with classes, and the errors are many. I am stuck with the error

Error CS0122 "Empenho.numero" is inacessible due the protect level
Can You help me to understand that error? I am copying the code below.

C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace folhaPensao
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //classes
            // new Empenho();
            //Empenho 31900301 = new Empenho();
            Empenho emp319003 = new Empenho();
            emp319003.numero = 40;
            //ajuda de custo
            //ushort empenho = 300;
            int ptres = 172256;
            int nd = 339093;
            uint fonte = 1020000059;
            decimal valor = 300m;
            decimal pss;
            decimal principal;
            pss = valor / 10 * 2;
            principal = valor - pss;         
        }
    }
}



file Empenho.cs:

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

namespace folhaPensao
{
    public class Empenho
    {
        int numero;

    }
}


C#:
file Empenho.cs:

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

namespace folhaPensao
{
    public class Empenho
    {
        int numero;

    }
}
 
Line 3 here declares a private field named numero. For C# classes, when you don't specify the visibility (e.g public, protected, private, etc.) then private is assumed by the compiler. This is a good thing because it encourages data encapsulation -- one of the key tenets of object oriented programming.
C#:
    public class Empenho
    {
        int numero;

    }

So when you have code in another class trying to access a private member of another class, it cannot:
C#:
emp319003.numero = 40;
 
What C# book or tutorial are you using? It should cover that aspect of visibility relatively early on when classes are introduced.
 

Latest posts

Back
Top Bottom