C#替换字符串
我试图运行此代码替换字符串,但是当我这样做,给了我一个错误说C#替换字符串
Error 1 A local variable named 'typeval' cannot be declared in this scope because it would give a different meaning to 'typeval', which is already used in a 'parent or current' scope to denote something else
这是代码
public static string Replace(string typeval)
{
string newType = "";
string typeval = "";
if (typeval.Equals("User Service Request"))
{
newType = typeval.Replace("User Service Request", "incident");
}
else if (typeval.Equals("User Service Restoration"))
{
newType = typeval.Replace("User Service Restoration", "u_request");
}
return newType;
}
您已经定义了typeval once
。你不能再声明它。
删除string typeval == ""
您还应该设置typval.Replace
到typval
而不是newType
。否则,你将总是返回一个空字符串。
最后你不需要if
陈述。您可以轻松简化功能,看起来像这样。
public static string Replace(string typeval)
{
typeval = typeval.Replace("User Service Request", "incident");
typeval = typeval.Replace("User Service Restoration", "u_request");
return typeval;
}
嗨,感谢您的快速回复 测试快速 – Deanvd 2013-04-09 15:37:15
它像梦一样工作谢谢你!!!!!! – Deanvd 2013-04-09 15:37:52
很高兴有效@Deanvd。 ;) – eandersson 2013-04-09 15:39:00
你有两个类型。一个在参数中,另一个在函数内部。重命名其中的一个
您正在声明与方法参数同名的本地变量。
您有两个名为'typeval'的变量,因此编译错误。
而这个错误信息不足以给出提示,也许你应该尝试重命名两个'typeval'中的一个? – Jon 2013-04-09 15:34:25
给变量typeval以不同的名称。错误是你已经在Parent中使用了带有这个名字的变量。只是改变这个变量的名称将解决你的问题 – 2013-04-09 15:34:37
它建议你在将来寻找这样的错误。 – 2013-04-09 15:38:18