-1

我正在尝试运行此代码来替换字符串,但是当我这样做时,它会给我一个错误提示

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;
}
4

4 回答 4

3

您已经定义了typeval once. 您不能再次声明它。

消除string typeval == ""

您还应该设置typval.Replacetypvaland not 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;
}
于 2013-04-09T15:33:01.910 回答
1

你有两个类型。一个在参数中,一个在函数中。重命名其中之一

于 2013-04-09T15:33:17.863 回答
0

您正在声明一个与方法参数同名的本地变量。

于 2013-04-09T15:33:21.117 回答
0

您有两个名为“typeval”的变量,因此会出现编译错误。

于 2013-04-09T15:33:38.387 回答