I am having problems calling a method in C#, I keep getting the message "Method (calculate) must have a return type".
我在调用C#中的方法时遇到问题,我不断收到消息“Method(calculate)必须有一个返回类型”。
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");
}
}
4 个解决方案
#1
3
calculate();
is an invalid method signature in your Hello
class. It is missing the return type and it also needs a body.
计算();是Hello类中的无效方法签名。它缺少返回类型,它还需要一个正文。
At a minimum the signature should look like:
签名至少应如下所示:
public class Hello : Form
{
public string test { get; set; }
void calculate() {}
}
#2
3
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.
无效,因为calculate()不是构造函数或方法。您无法从类范围调用方法。
#3
1
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.
那是因为你试图在一个类的主体内调用它。你不能在C#中做到这一点。您只能从其他方法或构造函数中调用方法。语法分析器认为您正在尝试定义新方法而忘记提及类型。
#4
0
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() {}
}
#1
3
calculate();
is an invalid method signature in your Hello
class. It is missing the return type and it also needs a body.
计算();是Hello类中的无效方法签名。它缺少返回类型,它还需要一个正文。
At a minimum the signature should look like:
签名至少应如下所示:
public class Hello : Form
{
public string test { get; set; }
void calculate() {}
}
#2
3
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.
无效,因为calculate()不是构造函数或方法。您无法从类范围调用方法。
#3
1
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.
那是因为你试图在一个类的主体内调用它。你不能在C#中做到这一点。您只能从其他方法或构造函数中调用方法。语法分析器认为您正在尝试定义新方法而忘记提及类型。
#4
0
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() {}
}