Why does my short code have so many errors?

BrunoB

Well-known member
Joined
Nov 19, 2022
Messages
96
Programming Experience
Beginner
Hello good morning, here is my brief code, thank you.
C#:
amespace Example1
{
class Program
{
static void Main(string[] args)
{
private int age;
private float salary;
private string name;


Person p1 = new Person();
p1.setAge(20);

Console.WriteLine("the person's age is" + p1.getAge());
Console.ReadKey();

Bottle b1 = new Bottle();
b1.Capacity = 200;

Console.WriteLine("the bottle has a capacity " + b1.Capacity());
Console.ReadKey();
 
C#:
clase Botella
{
private int capacidad;
private string color;

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

public int Capacidad
{
get { return capacidad; }
set { capacidad = valor; }
}

}
}
 
In the future, please post your code in code tags. It's the button that looks like </>.
 
If you post the errors that you are getting, you'll see that the errors are generally self-explanatory and point you to the source of the problem.
 
Anyways from post #1, it looks like you failed to declare your class Person and put lines 7-9 into that class declaration.

It also looks like your lines 18-19 refer to English language versions of a bottle class and its property, but your post #2 shows a Spanish version of that class and property.
 
In short, because it's full of syntax errors

1671484397215.png
 
Here are the differences (fixed on on the right):

1671484709745.png


Black = no change, red = difference. This diff tool is set to ignore whitespace differences
 
How could you end up with so many errors in the first place? It seems like every time an error was flagged, you just ignored it and kept on writing code. That said, the fact that you had "amespace" suggests that you copied and pasted with little skill and little understanding of what the code even supposed to do.
 
Anyways from post #1, it looks like you failed to declare your class Person and put lines 7-9 into that class declaration.

It also looks like your lines 18-19 refer to English language versions of a bottle class and its property, but your post #2 shows a Spanish version of that class and property.
what happens is that I write it in Spanish, but I translated it in translator so that you can understand it better...but it doesn't translate well.
 
Eahh, we don't really care about that. Just give us code full of Spanish variable names. As far as I know no one has ever translated c# itself (though I don't know why not) to have Spanish keywords so you will always have to write "public class Botella" rather than "clase pública Botella".

The only time I'd say to make an effort to rename your classes, properties, methods etc is when they are very similar and potentially confusing, but I make that recommendation in any language
 
I tried two ways, first I got the error you see in the screenshot, so I added the dot as suggested in the official error list and now, I get another error .... Why? thank you.

1671545330779.png
 
Propierties:
class Program
    {
        static void Main(string[] args)
        {
            Persona p1 = new Persona(); //CREACION DE OBJETO TIPO PERSONA
            p1.setEdad(20);

          


            Botella b1 = new Botella();
            b1.Capacidad = 250;
            

            Console.WriteLine("la capadida de la botella es  " + b1.capacidad);
            Console.ReadKey();

        }
    }
}
 
Back
Top Bottom