Question CS0122

BrunoB

Well-known member
Joined
Nov 19, 2022
Messages
96
Programming Experience
Beginner
Hello, good morning, I already did a search for that same error in this forum, it gave me 2 results... although from what I saw they suggest changing the variable to public and that would solve the error, but from what I understand this would be violate OOP principles.
So how can I solve it? I read on stack that it can be like this:
"This is usually solved using InternalsVisibleToAttribute."
But how correct is this? Thanks.
C#:
amespace Ejemplo1
{
    class Botella
    {
       private  int capacidad;
       private string color;
       private string material;
        private int cantidadActual;

        // EN VEZ DE USAR G USAMOS ALGO ESPECIAL DE C# LLAMADO PROPIEDAD

        public Botella( string color, string material)
        {
            this.color = color;
            this.material = material;
            this.capacidad = 100; // por que aca no pone this?
            cantidadActual = 0;

        }


        public string Material  // PROPERTY
        {
            get { return material; }
        }

        public int Capacidad
        {

            get { return capacidad; }
        }

        // PROPIEDAD SOL ODE LECTURA
        public int CantidadActual
        {

            get { return cantidadActual; }
        }


        public  float recargar()  //METODO RECARGA
        {
            if ( cantidadActual>0)
            {
                int dif = 100 - cantidadActual;
                //dif 100------50
                // dif -----  dif*50/100
                float monto = dif * 50 / 100;
                cantidadActual += dif;
                return monto;

            }
            cantidadActual = 100;
            return 50;

        }


        //public int Capacidad  // PROPERTY
        //{
        //    get { return capacidad; }
        //    set { capacidad = value; }
        //}


    }
}
 
Solution
From the outside you're supposed to use the public property public int CantidadActual, not the private field private int cantidadActual;.
Notice the case difference and compare with your attempt b1.cantidadActual
C#:
namespace Ejemplo1
{
    class Program
    {
        static void Main(string[] args)
        {
           // private int edad;
           //  private float sueldo;
           //private string nombre;


            //Persona p1 = new Persona();
            //p1.setEdad(20);

            //Console.WriteLine("la edad de la persona es" + p1.getEdad());
            //Console.ReadKey();

            Botella b1 = new Botella("rojo" , "plastico");
            //b1.Capacidad = 200;

            Console.WriteLine("la botella tiene una capacidad " + b1.Capacidad);
            Console.ReadKey();
            Console.WriteLine(" la cantidad actual que pose " + b1.cantidadActual);
            b1.recargar();
            Console.WriteLine("luego de la recarga" + b1.cantidadActual);


        }
    }
}
Caps with errors:
1673027764165.png

image.png
 
From the outside you're supposed to use the public property public int CantidadActual, not the private field private int cantidadActual;.
Notice the case difference and compare with your attempt b1.cantidadActual
 
Solution
image.png

From the outside you're supposed to use the public property public int CantidadActual, not the private field private int cantidadActual;.
Notice the case difference and compare with your attempt b1.cantidadActual
I mean, the official C# documentation is not 100% accurate...unfortunately
1673051231413.png
 
Getters and setters are methods under the hood. They are just presented as syntactic sugar to cut down on the boilerplate getters and setters usually seen in C++ and Java. So that error message documentation there is still right on the money regarding making a method public to make it accessible. The issue as shown in post #3 was that you were trying to access your backing field for your property instead of the public property itself.

Good instincts on your part though about making the backing field goes against good OOP principles.
 
Do not use case alone to distinguish members. Not all languages in the .NET ecosystem are case sensitive. Prefix your fields with an underscore; it will help comply with the "do not use case" rule and make the member sufficiently visually different that you will also not become confused into using the wrong thing (the field when you should be using the Property) either

C#:
public class Botella{
    private int _color;
    public int Color { get { return _color; } }

    public Botella(int color){
      _color = color;   //look, no need to use "this"
    }

    public override void ToString() {
      var localVariableName = 123;
      return $"Color is {_color}";
    }
}

ClassNames
PropertyNames
_fieldNames
MethodNames
argumentNames
localVariableNames

Doing this means you can look at some line of code on line 1000 and see:

_color = 4

And you can know "oh, it's a private class level field"

You aren't wondering "is that a field? Is it a local var? Is it both and I have to be careful whether I write "this" or not

Conventions established themselves to make life easier
 
Last edited:
Back
Top Bottom