简体   繁体   English

如何修复开关操作器中的错误

[英]How to fix errors in switch operator

I have a class named config with two string fields named key paramValue and parameterPath. 我有一个名为config的类,其中有两个名为key paramValue和parameterPath的字符串字段。

When I apply the ChooseType method of the class, the method has to return one variable paramValue in different types (Int or bool or String). 当我应用该类的ChooseType方法时,该方法必须返回一种不同类型(Int或bool或String)的变量paramValue。

I implemented it as follow: 我实现它如下:

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

         }
    }

}

But,I get error in switch operator in the next rows: 但是,我在下一行中切换操作符中出现错误:

 return int.Parse(string this.paramValue);

Error: 错误:

Only assignment, call, increment, decrement, and new object expressions can be used as a statement. 只能将赋值,调用,递增,递减和新对象表达式用作语句。

 return  Boolean.Parse(string this.paramValue);

Error: 错误:

Invalid expression term 'string'. 无效的表达式术语“字符串”。

 return this.paramValue;

Error: 错误:

Cannot implicitly convert type 'string' to 'T'. 无法将类型'string'隐式转换为'T'。

Any idea why do I get these errors and how can I fix the code? 知道为什么会出现这些错误以及如何修复代码吗?

Any idea why do I get these errors? 知道为什么我会收到这些错误吗?

The compiler has no idea what T will be, and it doesn't know how to implicitly convert from string , bool or int to T . 编译器不知道T是什么,也不知道如何从stringboolint隐式转换为T

and how can I fix the code? 以及如何修复代码?

You can go through an explicit conversion to object and then another to T : 您可以进行显式转换为object ,然后再转换为T

return (T) (object) int.Parse(string this.paramValue);

The requirement to go "via" object is a little odd - Eric Lippert has a blog post going through this in more detail . “通过” object的要求有点奇怪-Eric Lippert的博客文章对此进行了详细介绍

public T ChooseType<T>(RetType how)
{
    switch (how)
    {
        case RetType.RetInt:
            return (dynamic)int.Parse(paramValue);
        case RetType.RetBool:
            return (dynamic)Boolean.Parse(paramValue);
        case RetType.RetString:
            return (dynamic)paramValue;
        default:
            throw new ArgumentException("RetType not supported", "how");
    }
}

You should not specify parameter type when you are calling some method. 调用某些方法时,请勿指定参数类型。 Parameter types required only for method declaration. 仅方法声明所需的参数类型。 Thus just pass parameter: 因此只需传递参数:

int.Parse(this.paramValue) // you can use class members without this keyword
int.Parse(paramValue)

Also you should add default branch for your switch block (you must return something if incorrect parameter value was passed to your method). 另外,您还应该为switch块添加default分支(如果将不正确的参数值传递给您的方法,则必须返回一些内容)。

Also you don't need to break switch branches if you already used return . 另外,如果您已经使用过return则无需break开关分支。

And for converting some type to generic value you should use dynamic , or double conversion via object: 为了将某种类型转换为通用值,您应该使用dynamic ,或者通过object进行双重转换:

return (dynamic)int.Parse(paramValue);
return (T)(object)int.Parse(paramValue);

The problem is that you are stating that the return type of your function will be T. As T can be of ANY type, you can't explicitely return an int, string or any speciffic type. 问题是您要声明函数的返回类型将为T。由于T可以是ANY类型,因此您不能显式返回int,字符串或任何特殊类型。 You may want to try using one return instruction, something like 您可能想尝试使用一条返回指令,例如

public  T ChooseType<T>() 
{
    return (T)this.paramValue;
}

and then, when calling the function, specify T, like this: 然后,在调用函数时,指定T,如下所示:

int a = ChooseType<int>();

or 要么

string a = ChooseType<string>();

Keep in mind that if paramValue cannot be casted as T, then an error will be thrown. 请记住,如果无法将paramValue强制转换为T,则将引发错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM