Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

c# - Abstract - override methods return an error of "cannot convert void to bool"

I am trying to write a code that calculates the perimeter of the shapes in the process of learning abstract classes. So I created an abstract base class "Figure" and derive it with shape names. I use one method to override it with different implentations. PS: I know returning the methods would be easier and I could create a variable like "int sum" to store and print, but I'd like to use the way in the code I've given to grasp the problem. However, there is something wrong I can't see since yesterday. edit: "cannot explicitly convert void to bool" error are on lines 16 and 17 which are Figure rectangle = new Rectangle(5, 6); Figure triangle = new Triangle(4, 8, 3);

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

namespace CSHARP3
{
    class Program
    {
        static void Main(string[] args)
        {
            Figure rectangle = new Rectangle(5, 6);
            Figure triangle = new Triangle(4, 8, 3);

            Console.WriteLine(rectangle.Perimeter());
            Console.WriteLine(triangle.Perimeter());
        }
    }
    abstract class Figure
    {
        //define abstract method Perimeter
        public abstract void Perimeter();
    }
    class Rectangle : Figure
    {
        public int width;
        public int height;
        public Rectangle(int width, int height)
        {
            this.width = width;
            this.height = height;
        }
        //override Perimeter method for rectangle
        public override void Perimeter()
        {
            Console.WriteLine(2 * (width + height));
        }

    }
    class Triangle : Figure
    {
        public int side1;
        public int side2;
        public int side3;
        public Triangle(int s1, int s2, int s3)
        {
            this.side1 = s1;
            this.side2 = s2;
            this.side3 = s3;
        }

        //override Perimeter method for triangle
        public override void Perimeter()
        {
            Console.WriteLine(side1 + side2 + side3);
        }

    }
}
question from:https://stackoverflow.com/questions/66065509/abstract-override-methods-return-an-error-of-cannot-convert-void-to-bool

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your line Console.WriteLine(rectangle.Perimeter()); you are calling Console.WriteLine on a method that returns nothing. In fact that Perimeter() is doing a Console.WriteLine itself!

The best fix would be to return a value from that method:

abstract class Figure
{
    public abstract int Perimeter();
}

and of course fix the actual implementations by replacing the Console.WriteLine with a return.

If you really really want that method to do its own printing, then just call the method: rectangle.Perimeter(); No extra Console.WriteLine needed, as that method already takes care of it (and there is nothing to print anyway).


As to why the compiler's error message (CS1503) mentions "bool": Console.WriteLine has multiple overloads and none of them fit. The compiler chooses one (apparently with a bool argument) and complains that your argument (type void) doesn't fit.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...