【问题标题】:Type of conditional expression cannot be determined because there is no implicit conversion between 'int' and 'string'无法确定条件表达式的类型,因为 'int' 和 'string' 之间没有隐式转换
【发布时间】:2021-12-10 19:57:12
【问题描述】:

请这里的任何人可以帮助我解决这个问题

checked
{
    foreach (string text2 in ParametersString)
    {
        int num = Conversions.ToInteger((!Versioned.IsNumeric(text2)) ? -1 : text2);
        if (num == -1)
        {
            Interaction.MsgBox(string.Format("Entering characters is not allowed ---> {0}", text2), MsgBoxStyle.Critical, Class10.string_4);
        }
        else if (!this.method_2(num))
        {
            Interaction.MsgBox(string.Format("The specified port ({0}) is already in use", text2), MsgBoxStyle.Critical, Class10.string_4);
            Environment.Exit(0);
        }
        else
        {
            this.Listener = new TcpListener(IPAddress.Any, num);
            this.Listener.Server.SendTimeout = -1;
            this.Listener.Server.ReceiveTimeout = -1;
            this.Listener.Server.SendBufferSize = 999999;
            this.Listener.Server.ReceiveBufferSize = 999999;
            this.Listener.Start();
            int num2 = 5;
            string string_ = Class7.string_0;
            if (Operators.CompareString(string_, "High", false) != 0)
            {
                if (Operators.CompareString(string_, "Normal", false) == 0)
                {
                    num2 = 10;
                }
                else if (Operators.CompareString(string_, "Low", false) == 0)
                {
                    num2 = 5;
                }
            }
            else
            {
                num2 = 16;
            }
            int num3 = num2;
            for (int j = 1; j <= num3; j++)
            {
                new Thread(new ThreadStart(this.ScanerAsync)).Start();
            }
            int num4 = num2;
            for (int k = 1; k <= num4; k++)
            {
                new Thread(new ThreadStart(this.method_1)).Start();
            }
            text += Conversions.ToString(num);
            this.closing = false;
        }
    }
    Class6.string_0 = text;
}

线路错误

int num = Conversions.ToInteger((!Versioned.IsNumeric(text2)) ? -1 : text2);

错误:

无法确定条件表达式的类型,因为'int'和'string'之间没有隐式转换

【问题讨论】:

    标签: c#


    【解决方案1】:

    无法确定条件表达式的类型,因为'int'和'string'之间没有隐式转换

    这意味着!Versioned.IsNumeric(text2)) ? -1 : text2 的返回类型不能缩小为单一类型。它可以是 int (-1) 或 string (text2)。

    将两者都视为字符串

    int num = Conversions.ToInteger((!Versioned.IsNumeric(text2)) ? "-1" : text2);
    

    现在,无论哪种情况,条件三元运算符的结果都是字符串

    在外面应用条件

    int num = Versioned.IsNumeric(text2) ? Conversions.ToInteger(text2) : -1;
    
    • 如果text2 是数字,则将字符串转换为整数
    • 如果text2 不是数字,则使用-1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-18
      • 1970-01-01
      • 2018-03-14
      • 1970-01-01
      • 2018-09-27
      • 2016-03-08
      相关资源
      最近更新 更多