开发者

C# - Method must have a return type

I am having problems calling a method in C#, I keep getting the message "Method (calculate) must have a return type".

using System.Diagnostics;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }
    }



    public class Hello : Form
    {
        public string test { get; set; }
        calculate();
    }


    public class Hello2 : Form
    {开发者_高级运维
        public void calculate()
        {
            Process.Start("test.exe");

        }
    }


calculate(); is an invalid method signature in your Hello class. It is missing the return type and it also needs a body.

At a minimum the signature should look like:

public class Hello : Form
{
    public string test { get; set; }
    void calculate() {}
}


public class Hello : Form
{
    public string test { get; set; }
    **calculate();**
}

Is not valid because calculate() is not a constructor or method. You cannot call methods from the class scope.


if calculate doesn't return anything you have to be explicit and say that with void.

It also needs a method body (unless it is marked as abstract).

public class Hello : Form
{
    public string test { get; set; }
    void calculate() {}
}


That's because you are trying to call it inside the body of a class. You cannot do this in C#. You can only call methods from other methods or constructors. The syntax parser thinks that you are trying to define a new method and forgot to mention the type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜