Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VB.NET中的一行if_Vb.net_Conditional Operator - Fatal编程技术网

VB.NET中的一行if

VB.NET中的一行if,vb.net,conditional-operator,Vb.net,Conditional Operator,在VB.NET中是否可以执行一行if语句?如果是,怎么做?只需添加,然后: If A = 1 Then A = 2 或: 其实很简单 If CONDITION Then ..INSERT CODE HERE.. 或 您也可以使用IIf功能: CheckIt = IIf(TestMe > 1000, "Large", "Small") 使用IF() 它是一个短路三值运算符 Dim Result = IF(expression,<true return>,<false

在VB.NET中是否可以执行一行if语句?如果是,怎么做?

只需添加
,然后

If A = 1 Then A = 2
或:


其实很简单

If CONDITION Then ..INSERT CODE HERE..


您也可以使用IIf功能:

CheckIt = IIf(TestMe > 1000, "Large", "Small")
使用IF()

它是一个短路三值运算符

Dim Result = IF(expression,<true return>,<false return>)
Dim Result=IF(表达式,,)
另见:


在VB中,您可以使用多个语句,或者在一行if语句中使用多个语句,这可能会引起purest和c#程序员的反感。在本例中,y以3结尾,而不是7

i = 1
If i = 1 Then x = 3 : y = 3 Else x = 7 : y = 7

它在VB.NET代码中易于使用

基本语法 IIF(表达式为布尔值,真部分为对象,假部分为对象)为对象

  • 使用与三元相同的IIF
  • Dim myVariable as string=“”
  • myVariable=IIf(条件、真、假)

  • 不知道为什么人们还没有发布这个

    单行

    语法:

    If (condition) Then (do this)
    
    If (condition) Then : (do this)
    ElseIf (condition2) Then : (do this)
    Else : (do this)
    End If
    
    If (condition) Then : (do this) : (and this) : End If
    
    例如:

    If flag = true Then i = 1
    
    多个ElseIf

    If <C1> Then <R1> Else If <C2> Then <R2> Else <E>
    
    If <C1> Then <R1> Else If <C2> Then <R2> If <C3> Then <R3> If <C4> Then <R4> Else...
    ' Just keep adding "If <C> Then <R> Else" to get more
    
    语法:

    If (condition) Then (do this)
    
    If (condition) Then : (do this)
    ElseIf (condition2) Then : (do this)
    Else : (do this)
    End If
    
    If (condition) Then : (do this) : (and this) : End If
    

    多个操作

    If <C1> Then <R1> Else If <C2> Then <R2> Else <E>
    
    If <C1> Then <R1> Else If <C2> Then <R2> If <C3> Then <R3> If <C4> Then <R4> Else...
    ' Just keep adding "If <C> Then <R> Else" to get more
    
    语法:

    If (condition) Then (do this)
    
    If (condition) Then : (do this)
    ElseIf (condition2) Then : (do this)
    Else : (do this)
    End If
    
    If (condition) Then : (do this) : (and this) : End If
    
    希望这能对某人有所帮助。

    一行“如果声明” 比你想象的容易,注意到还没有人把我的东西放进去,所以我会把我的2美分扔进去

    在我的测试中,您不需要
    继续?分号
    ,可以不使用,也可以不使用
    结束If

    =条件。

    =真实返回。

    =否则返回。

    单一条件

    If <C1> Then <R1> Else <E>
    
    If-Then-Else
    
    多个条件

    If <C1> Then <R1> Else If <C2> Then <R2> Else <E>
    
    If <C1> Then <R1> Else If <C2> Then <R2> If <C3> Then <R3> If <C4> Then <R4> Else...
    ' Just keep adding "If <C> Then <R> Else" to get more
    
    If Then Else If Then Else
    
    无限?条件

    If <C1> Then <R1> Else If <C2> Then <R2> Else <E>
    
    If <C1> Then <R1> Else If <C2> Then <R2> If <C3> Then <R3> If <C4> Then <R4> Else...
    ' Just keep adding "If <C> Then <R> Else" to get more
    
    If-Then-Else-If-Then-Else。。。
    只需继续添加“If Then Else”即可获得更多信息
    
    -我真的不知道如何格式化以使其更具可读性,所以如果有人可以提供编辑,请这样做-

    在较长的版本中,它将如下所示:

    If (condition_is_true) Then 
    
    Else (condition_is_false)
    
    End If
    

    if Condition then command1:else command2…

    如果此表达式返回结果true或false,则可以使用iif,一行

    ,这是一个表达式,而问题需要一个语句。;-)与
    If
    相同的语法相比,
    IIf
    的缺点是
    IIf
    更容易进行隐式类型转换。例如:
    If(x.HasValue,x.Value,Nothing)
    如果x不包含值,则返回0。一种补救方法是强制将值设置为可空,如下所示:
    If(x.HasValue,CType(x.value,Nullable(of Integer)),Nothing)
    @LosManos仅供参考,得出结果的原因如下:如果
    x
    具有type
    Integer?
    ,则
    x.value
    具有type
    Integer
    。因此,编译器正确地将零转换为
    整数
    (以匹配其他结果),从而得到
    0
    。只是解释编译器的行为;你所做的是你想要的一个很好的解决方案。另一种解决方案是将Nothing强制转换为所需的类型,例如
    DirectCast(Nothing,Integer?)
    。你是说If在返回ExpressionIfFalse时检查ExpressionIfTrue返回的类型吗?但要小心IIf运算符-它并不总是这样,而且对真表达式和假表达式都进行了计算。我实际上认为IIf从不短路IFF比If有什么好处,甚至目的?@LeoKing与VB6的向后兼容性。VB.NET中引入了单“I”IF,而以前存在双“I”IIF。我不相信VB6的任何部分支持短路评估。是的。尽管我讨厌这样,但将
    If…Then…Else…
    语句放在一行是可能的。但是,当您将它与使用冒号将多个语句放在一行结合使用时,请注意。语句
    If-Then:
    将仅结合操作1执行操作2!Action2本身不是一个单独的声明!所以为了清楚起见,我建议始终使用块语句,并避免使用冒号。@BrianWebster-Nice。我讨厌它嗨,莫斯塔法,欢迎来到斯塔克沃夫!你的答案很短,没有任何解释,也没有在其他答案中添加任何附加信息,比如Fluffy Sebbert的答案。