【问题标题】:What is the VB.NET equivalent of the C# ? operator? [duplicate]什么是 C# 的 VB.NET 等价物?操作员? [复制]
【发布时间】:2013-05-17 09:55:30
【问题描述】:

VB.NET 等效于 C# ? 运算符是什么?

比如下面的代码怎么写成VB.NET

hp.pt = iniFile.GetValue("System", "PT").ToUpper().Equals("H") ? PT.PA : PT.SP

【问题讨论】:

    标签: c# .net vb.net iif


    【解决方案1】:

    从历史上看,IIf 通常用于此目的 - 但它不使用短路,因此并不完全相同。但是,there is now a 3-part If

    hp.pt = If(iniFile.GetValue("System", "PT").ToUpper().Equals("H"), PT.PA, PT.SP)
    

    确实使用短路,因此与 C# 中的 conditional operator 相同。

    【讨论】:

      【解决方案2】:

      您可以使用If operator

      hp.pt = If(iniFile.GetValue("System", "PT").ToUpper().Equals("H"), PT.PA, PT.SP)
      

      【讨论】:

        【解决方案3】:

        尝试像这样使用If 函数:

        x = If(condition, trueValue, falseValue)
        

        【讨论】:

          【解决方案4】:

          此问题与已经提出并回答的问题重复:

          Is there a conditional ternary operator in VB.NET?

          这里:

          Dim foo as String = If(bar = buz, cat, dog)
          

          【讨论】:

          • 你应该将问题标记为重复。
          • 届时会这样做
          猜你喜欢
          • 1970-01-01
          • 2010-09-29
          • 2019-02-09
          • 1970-01-01
          • 2015-06-04
          • 2011-05-23
          • 1970-01-01
          • 2015-07-10
          • 1970-01-01
          相关资源
          最近更新 更多