下面是我正在处理的代码的一部分,执行时出现以下错误。
“无法将类型void隐式转换为字符串”。
我该如何解决?
private static void Main(string[] args)
{
string[] strArray = new string[n];
int i = 0;
foreach (example)
{
if (condition 1)
{
strArray[i] = output;
}
if (condition 2)
{
strArray[i] = output;
}
if (condition 3)
{
strArray[i] = output;
}
i = i + 1;
}
}
答案 0 :(得分:4)
当我向您展示代码
private static void Main(string[] args) { string[] strArray = new string[n]; int i = 0; foreach (example) { if (condition 1) { strArray[i] = output; } if (condition 2) { strArray[i] = output; } if (condition 3) { strArray[i] = output; } i = i + 1; } }
进入.net小提琴,我得到:
Compilation error (line 1, col 16): Expected class, delegate, enum, interface, or struct
Compilation error (line 1, col 33): Identifier expected
Compilation error (line 1, col 35): Expected class, delegate, enum, interface, or struct
Compilation error (line 3, col 12): Identifier expected
Compilation error (line 3, col 14): Expected class, delegate, enum, interface, or struct
Compilation error (line 3, col 29): Expected class, delegate, enum, interface, or struct
Compilation error (line 3, col 38): Expected class, delegate, enum, interface, or struct
Compilation error (line 10, col 25): Expected class, delegate, enum, interface, or struct
Compilation error (line 11, col 9): Type or namespace definition, or end-of-file expected
用
环绕public class MyClass
{
// your code
}
让我离开
Compilation error (line 8, col 21): Identifier expected
Compilation error (line 8, col 22): ) expected
Compilation error (line 10, col 23): ) expected
Compilation error (line 10, col 24): ; expected
Compilation error (line 10, col 24): Invalid expression term ')'
Compilation error (line 10, col 25): ; expected
Compilation error (line 14, col 23): ) expected
Compilation error (line 14, col 24): ; expected
Compilation error (line 14, col 24): Invalid expression term ')'
Compilation error (line 14, col 25): ; expected
Compilation error (line 18, col 23): ) expected
Compilation error (line 18, col 24): ; expected
Compilation error (line 18, col 24): Invalid expression term ')'
Compilation error (line 18, col 25): ; expected
将丢失的n
和foreach
固定到
string[] strArray = new string[10];
int i = 0;
var data = new [] {"1","2","3","4"};
foreach (var example in data)
{
// your inner code
}
让我离开
Compilation error (line 11, col 23): ) expected
Compilation error (line 11, col 24): ; expected
Compilation error (line 11, col 24): Invalid expression term ')'
Compilation error (line 11, col 25): ; expected
Compilation error (line 15, col 23): ) expected
Compilation error (line 15, col 24): ; expected
Compilation error (line 15, col 24): Invalid expression term ')'
Compilation error (line 15, col 25): ; expected
Compilation error (line 19, col 23): ) expected
Compilation error (line 19, col 24): ; expected
Compilation error (line 19, col 24): Invalid expression term ')'
Compilation error (line 19, col 25): ; expected
修正内部条件
if (i==1)
{
strArray[i] = "There is";
}
if (i==2)
{
strArray[i] = "is no";
}
if (i==3)
{
strArray[i] = "no spoon.";
}
i = i + 1;
导致缺少公共静态Main,而导致
的修复public class MyClass
{
public static void Main(string[] args)
{
string[] strArray = new string[10];
int i = 0;
var data = new[]{"1", "2", "3", "4"};
foreach (var example in data)
{
if (i == 1)
{
strArray[i] = "There is";
}
if (i == 2)
{
strArray[i] = "is no";
}
if (i == 3)
{
strArray[i] = "no spoon.";
}
i = i + 1;
}
}
}
运行无错误的程序。
在旅途中的任何地方都没有弹出您的错误消息。
答案 1 :(得分:0)
如果 output 是具有void返回类型的函数,则不能将其值分配给strArray [i]。在每种情况下,请确保函数的返回值都不为空。
此外,问题可能出在使用 foreach ,因此将其替换为for 循环,因为更改 foreach 的枚举数可能会产生一个问题。
答案 2 :(得分:0)
尝试删除string[] args
内的空白类型或Main(string[] args)
(不是其他解决方案)(但必须解决),但必须改进的地方
if(condtion==1){...}
if(condtion==2){...}
if(condtion==3){...}
等
更好的做法是将其替换为Switch,这样程序每次调用方法时都不会检查很多ifs