代码之家  ›  专栏  ›  技术社区  ›  Raúl Roa

VB.NET中的一行if

  •  65
  • Raúl Roa  · 技术社区  · 17 年前

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

    10 回复  |  直到 14 年前
        2
  •  25
  •   Quintin Robinson    17 年前

    其实很简单。。

    If CONDITION Then ..INSERT CODE HERE..
    
        3
  •  18
  •   Tomerikoo Glorified    5 年前

    单线

    语法:

    If (condition) Then (do this)
    

    例子:

    If flag = true Then i = 1
    

    语法:

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

    或

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

    多重操作

    If (condition) Then : (do this) : (and this) : End If
    
        4
  •  16
  •   xpda    17 年前

    在VB中,您可以使用多个语句,也可以在一行if语句中使用其他语句,这可能会引起纯粹的程序员和c#程序员的不满。在本例中,y以3结尾,而不是7。

    i = 1
    If i = 1 Then x = 3 : y = 3 Else x = 7 : y = 7
    
        5
  •  7
  •   Dmitriy Zhukov    17 年前

    或

    IIf(CONDITION, TRUE_ACTION, FALSE_ACTION)
    
        6
  •  4
  •   Anton Gogolev    17 年前

    Then :

    If A = 1 Then A = 2
    

    或:

    If A = 1 Then _
        A = 2
    
        7
  •  4
  •   nora    10 年前

    一行“If语句”

    比你想象的容易,注意到还没有人把我的东西放进去,所以我会把我的2美分扔进去。

    在我的测试中,你不需要 continuation? semi-colon ,你可以不用,也可以不用 End If .

    <C#> = Condition.

    <R#> = True Return.

    <E> = Else Return.

    If <C1> Then <R1> Else <E>
    

    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
    

    -我真的不知道如何格式化以使其更具可读性,所以如果有人可以提供编辑,请这样做-

        8
  •  3
  •   Brian Tompsett - 汤莱恩 andrewwong97    10 年前
    If (X1= 1) Then : Val1= "Yes" : Else : Val1= "Not" : End If
    
        9
  •  1
  •   Jon Limjap    17 年前

    CheckIt = IIf(TestMe > 1000, "Large", "Small")
    
        10
  •  0
  •   Rashi    12 年前

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

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

    1. 使用与三元相同的IIF
    2. Dim myVariable as string=“”
        11
  •  0
  •   ravarador    7 年前
    If (condition, condition_is_true, condition_is_false)
    

    If (condition_is_true) Then 
    
    Else (condition_is_false)
    
    End If