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

namespace Lesson02
{
    class Rectangle
    {
        private double length; //default access level
        private double width;
        public Rectangle(double l, double w)
        {
            length = l;
            width = w;
        }
        public double GetArea()
        {
            return length * width;
        }
    }
}

    class Program
{
    static void Main(string[] args)
    {
        Rectangle rect = new Rectangle(10.0, 20.0);
        double area = rect.GetArea();
        Console.WriteLine("Area of Rectangle: {0}", area);
    }
}

I am getting errors relating to the use of Rectangle Severity Code Description Project File Line Suppression State Error CS0246 The type or namespace name 'Rectangle' could not be found (are you missing a using directive or an assembly reference?) Lesson02 C:\Users\jbond\source\repos\Lesson02\Lesson02\Program.cs 29 Active

Can anyone please help? Many thanks.

  • Your class is in the namespace `Lesson02` but your `Main` function is not. if you move that it & change the start method of your program or reference the `Rectangle` class with the namespace it should work. – TZHX May 01 '18 at 08:14
  • add `using Lesson02` directive to the file where you have the `Program class` defined – NoviceProgrammer May 01 '18 at 08:23

2 Answers2

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

namespace Lesson02
{
    class Rectangle
    {
        private double length; //default access level
        private double width;
        public Rectangle(double l, double w)
        {
            length = l;
            width = w;
        }
        public double GetArea()
        {
            return length * width;
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        Lesson02.Rectangle rect = new Lesson02.Rectangle(10.0, 20.0);
        double area = rect.GetArea();
        Console.WriteLine("Area of Rectangle: {0}", area);
    }
}

You need to call class Rectangle from namespace

0

You put your Rectangle class in a namespace but your Program class is outside it. Therefore you must either fully quality the class name as Lesson02.Rectangle in your Main function, or put using Lesson02; at the top, or (best option) put your Program class inside the namespace as well.

Matti Virkkunen
  • 63,558
  • 9
  • 127
  • 159