且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法将类型void隐式转换为int

更新时间:2021-11-11 15:01:21

您的方法 target.docalc()是一个空方法,而 actual 是一个int.如编译器所说,您不能将 void 分配给 int .

Your method target.docalc() is a void method, while actual is an int. You can't assign void to an int, as the compiler says.

根据您的评论(您实际上应该只编辑您的问题),您的 docalc()看起来像这样:

According to your comment (you really should just edit your question), your docalc() looks like this:

public void docalc(double n1, double n2, int op) 
{   
    result = 0; 

    ...

    setText(result.ToString());
}

您必须将方法的返回类型更改为 int ,然后返回结果:

You'll have to change the return type of the method to int, and return result:

public int docalc(double n1, double n2, int op) 
{   
    int result = 0; 

    ...

    return result;
}

旁注,为什么要这么做?

Sidenote, why do you do this?

[TestMethod()]
public void docalcTest(int actual)
{
     ...

    actual = ...

将在不带参数的情况下调用测试方法,因此该方法有点用处.您可能需要将其更改为:

The test method will be called without parameters, so it's a bit useless there. You might want to change it to:

[TestMethod()]
public void docalcTest()
{
     ...

    int actual = ...