【发布时间】:2012-07-16 11:53:53
【问题描述】:
我有一个名为 config 的类,其中包含两个字符串字段,分别名为 key paramValue 和 parameterPath。
当我应用类的ChooseType方法时,该方法必须返回一个不同类型(Int或bool或String)的变量paramValue。
我实现如下:
class ConfigValue
{
public string parameterPath;
private string paramValue;
public ConfigValue(string ParameterPath="empty",string ParamValue="empty")
{
this.parameterPath = ParameterPath;
this.paramValue = ParameterPath;
}
public enum RetType { RetInt=1, RetBool, RetString };
public T ChooseType<T>(RetType how)
{
{
switch(how)
{
case RetType.RetInt:
return int.Parse(string this.paramValue);
break;
case RetType.RetBool:
return Boolean.Parse(string this.paramValue);
break;
case RetType.RetString:
return this.paramValue;
break;
}
}
}
}
但是,我在下一行的 switch 运算符中遇到错误:
return int.Parse(string this.paramValue);
错误:
只有赋值、调用、递增、递减和新对象表达式可以用作语句。
return Boolean.Parse(string this.paramValue);
错误:
表达式术语“字符串”无效。
return this.paramValue;
错误:
无法将类型“字符串”隐式转换为“T”。
知道为什么会出现这些错误以及如何修复代码吗?
【问题讨论】:
-
"return int.Parse(string this.paramValue)" 不是一个有效的语句。应该是“return int.Parse(this.paramValue)”
-
svanryckeghem,在这种情况下,我收到此错误:无法将类型“int”隐式转换为“T”。