0

I'm having a few issues while trying use Inheritance. I create a class named vehicle with some properties, and the another one named car which inherits the properties from vehicle, them when I run the code below, the C# compiler returns the following error:

Program.cs (38,13): error CS0246: The namespace name or type 'car' could not be found. Need a policy using or an assembly reference?

Here's the code:

using System;

class vehicle
{
    public int MaxSpeed;
    public bool turnOn;
    public int wheels;

    public void car_on()
    {
        turnOn = true;
    }

    public void car_off()
    {
        turnOn = false;
    }

    class car : vehicle
    {
        public string name;
        public string color;

        public car(string name, string color)
        {
            this.name = name;
            this.color = color;
            MaxSpeed = 220;
            wheels = 4;
            turnOn();
        }
    }
}

namespace Aula_28_herança
{
    class Program
    {
        static void Main(string[] args)
        {
            car c1= new car("ferrari","red");

            Console.WriteLine("Nome................:{0}", c1.name);
            Console.WriteLine("Cor.................:{0}", c1.color;
            Console.WriteLine("Velociade Máxima....:{0}", c1.MaxSpeed);
            Console.WriteLine("Quantiadade de Rodas:{0}", c1.wheels);
            Console.WriteLine("Status..............:{0}", c1.turnOn);
        }
    }
}
1
  • 1
    Your class car is nested inside vehicle. That is not useful and not recommended. Put them in separate files in the same folder. Commented Jun 21, 2020 at 14:21

1 Answer 1

1

You need to make your carro type public:

class veiculo{
....

    public class carro : veiculo{
      ...
    }
}

Ad use veiculo.carro for type:

veiculo.carro c1= new veiculo.carro("ferrari","vermelha");

See the docs on Nested Types:

Regardless of whether the outer type is a class, interface, or struct, nested types default to private; they are accessible only from their containing type.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.