Why is this namespace unable to be found?

51 Views Asked by At

Fan.cs

using System;

namespace Fans {
    
    class Fan {
        private int speed;
        private double radius;
        private string color;

        //default constructor
        public Fan() {
            this.speed = 1;
            this.radius = 1.53;
            this.color = "green";
        }

        //convenience constructor
        public Fan(double newRadius) {
            this.radius = newRadius;
        }

        public override string ToString() {
            return "A " + radius + " inch " + color " fan at a speed of " + speed;
        }

        public int speed {
            get { return speed; }
            set { speed = newSpeed; }
        }

        public double radius {
            get { return radius; }
            set { radius = newRadius; }
        }

        public string color {
            get { return color ; }
            set { color = newColor; }
        }
    }

}

Program.cs

using System;

namespace Fans {
    class Program {
        
        static void Main(string[] args) {
        Fan fan1 = new Fan();
        fan1.speed = 3;
        fan1.radius = 10.26;
        fan1.color = "yellow";

        Console.WriteLine(fan1);
        }
    }
}

When I attempt to compile Program.cs, I get two errors:

Program.cs(7,9): error CS0246: The type or namespace name 'Fan' could not be found (are you missing a using directive or an assembly reference?)

Program.cs(7,24): error CS0246: The type or namespace name 'Fan' could not be found (are you missing a using directive or an assembly reference?)

Both files are in the same directory. Not sure what else is wrong.

1

There are 1 best solutions below

0
mike On

There are a few minor things to fix here.

  • You are missing a + in ToString

  • You need to use value in your set instead of newRadius, newColor and newSpeed

  • You are using the same member name as your variable. In the old days we would use underscore like I have in the working example below. Consider using the new format though

    public int Speed { get; set; }
    

Full code:

using System;

namespace Fans {

    class Fan {
        private int _speed;
        private double _radius;
        private string _color;
        //default constructor
        public Fan() {
            this._speed = 1;
            this._radius = 1.53;
            this._color = "green";
        }
        //convenience constructor
        public Fan(double newRadius) {
            this._radius = newRadius;
        }

        public override string ToString() {
            return "A " + _radius + " inch " + _color + " fan at a speed of " + _speed;
        }

        public int speed {
            get { return _speed; }
            set { _speed = value; }
        }

        public double radius {
            get { return _radius; }
            set { _radius = value; }
        }

        public string color {
            get { return _color ; }
            set { _color = value; }
        }
    }
}