Question

I'm working on some code for a school project, where we are to have a class defining a circle and a method from which it draws, my code so far returns "The namespace 'console' already contains a definition for 'Circle' here is the code:

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

namespace console
{
    class Program
    {

        static void Main(string[] args)
        {

            Console.Write("What is the circle’s radius: ");
            double radius = Convert.ToDouble(Console.ReadLine());
            Circle ans = new Circle(radius);


            Console.WriteLine("The area of the circle is " + ans.getArea);
            Console.WriteLine("The Diameter of the circle is " + ans.getDiameter);
            Console.WriteLine("The Circumference of the circle is " + ans.getCircumference);

            Console.Write("Enter any character to quit program. ");
            double stop = Console.Read();

        }
    }
}

That's the method and the class:

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

namespace console
{
    class Circle
    {

 public double radius;
            public const double PI = 3.14159;
            public Circle(double r)
            {
            radius = r;
        }
        public Circle()
        {
            radius = 0.0;
        }

        public double getDiameter
        {
            get
            {
                return radius * 2;
            }

        }


        public double getArea
        {
            get
            {
                return PI * radius * radius;
            }

        }
        public double getCircumference
        {
            get
            {
                return 2 * PI * radius;
            }

        }
        // property Radius 
        public double Radius
        {
            get
            {
                return radius;
            }

            set
            {
                // ensure non-negative radius value 
                if (value >= 0)
                    radius = value;
            }
        }
    }
}

One final question for this, now that I have the working code, the set and get are supposed to not allow negative input. However, when this is run, negative input still returns results, what am I missing that is causing the negative values to still be calculated?

Was it helpful?

Solution 2

The error is quite specific. You've declared two classes with the same name Circle in the same namespace. You can't do that. The compiler needs to be able to distinguish between the two classes, how can it do that if they have the same name?

I would recommend changing the name of the class that contains your Main entry point to something more appropriate, such as Program.

Also, as good practice you should stick to generally accepted naming conventions and name your namespace Console, rather than console.

OTHER TIPS

(Compiler output messages are your friends: do learn to understand them.)

You cannot have two classes named the same thing in the same namespace.

Circle appears twice in the console namespace.

Just rename one of them. If I were you I'd change the first one as it's not really modelling a circle. The second class is however.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top