0

I'm a C# beginner (3-day experience). Today I'm practising the inheritance in C#. Here is my code:

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

namespace Geometry
{
public class Geometry
{
    public double perimeter;
    public double area;
    public bool special;
}

public class Rect : Geometry
{
    public void Rectangle( double length, double width )
    {
        perimeter = (length + width) * 2;
        area = length * width;
        special = (length == width) ? true : false;

        Console.WriteLine("Rectangle information: ");
        Console.WriteLine("Perimeter: " + perimeter);
        Console.WriteLine("Area: " + area);
        if (special) Console.WriteLine("This is a special rectangle. In fact, this is a square!");
        Console.WriteLine();
    }
}

public class Cir : Geometry
{
    public void Circle(double radius)
    {
        perimeter = radius * 2 * Math.PI;
        area = radius * radius * Math.PI;

        Console.WriteLine("Perimeter: " + perimeter);
        Console.WriteLine("Area: " + area);
        Console.WriteLine();
    }
}

public class Main
{
    public static void main(String[] args)
    {
        Rectangle a = new Rectangle(23.00, 21.4);
        Circle b = new Circle(5.8);
    }
}

The compiler said that "The type or namespace of 'Rectangle' could not be found", same as Circle. What's wrong? I don't really understand although I tried many ways to fix them. How can I find and understand the errors?

4
  • 7
    Your classes are called Rect and Cir. Those are their names. Use their names, not different names. Commented Jun 12, 2017 at 18:38
  • Adding to @EdPlunkett comment, not only that but you also specify void in your constructor definitions. This is incorrect, it should be simply public Rectangle(double length, double width) (or Rect instead since that's the actual class name) and public Circle(double radius) (again, or Circ). Commented Jun 12, 2017 at 18:43
  • @sab669 Rectangle is not the name of the class. I think you're right that he intended those to be constructors, but he needs to name them correctly as well. Commented Jun 12, 2017 at 18:44
  • 1
    @EdPlunkett Yea I just editted my comment noting that :/ Commented Jun 12, 2017 at 18:45

6 Answers 6

6

Read the error. You never wrote a class named Rectangle. Rect is not the same thing.

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

Comments

3

You have 2 errors.

Error #1 is that your constructor name and your class name are not the same

Error #2 is that the constructor cannot have a return type (void)

public class Rectangle
{
    public Rectangle(double length, double width)
    {
       ...
    }
}

Comments

1

I think you should focus some of your learning on knowing the difference between a class and a method.

In your code you have created the following two classes:

public class Cir : Geometry

public class Rect : Geometry

Yet when you instantiate the classes in your Main() method you do not call them by their class name, instead by the void methods you have defined for each:

Rectangle a = new Rectangle(23.00, 21.4);
Circle b = new Circle(5.8);

What you likely want to do is rename both classes to Rectangle and Circle add a Constructor to each class that takes the parameters you want.

public class Circle : Geometry
{
    public Circle(double radius)
    {
        perimeter = radius * 2 * Math.PI;
        area = radius * radius * Math.PI;
    }
}

Try to keep your methods light, restricting them to a single responsibility or purpose and name the accordingly. For instance in your Geometry class, you could add a void PrintDetails() method or even better a void PrintArea() AND a void PrintPerimeter() method:

public class Geometry
{
    public double perimeter;
    public double area;
    public bool special;

    public void PrintArea()
    {
        Console.WriteLine("Area: " + area);
    }

    public void PrintPerimeter()
    {
        Console.WriteLine("Perimeter: " + perimeter);
    }
}

1 Comment

What an explicit answer! Thanks for your help <3
0

You can only create instances/objects of classes. Your class names are

Rect 

and

Cir 

Your Object names of these classes here are

a

and

b

respectively. so Your code should be

 Rect  a = new Rect; 
 a.Rectangle(23.00, 21.4); //Calls your method using Rect class object
 Cir  b = new Cir; 
 b.Circle(5.8);//Calls your method using Cir class object

Comments

-1

The class you define is "Rect" as example. Now while you may think that is an abbreviation to "Rectangle", the compiler has a more altruistic point of view - it is NOT the same, so error.

Same with "Circ".

Now, both classes have CONSTRUCTORS with the proper name (which thus are not constructors) but the class names themselves - are wrong.

Comments

-1

You did not closed the namespace geometry. You can create an instance of Rect(class) or Cir, not Rectangle(method) or Circle.

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.