vb.net 一行如果在 VB .NET 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/771273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
One line if in VB .NET
提问by Raúl Roa
Is it possible to do one line if statement in VB .NET? If so, how?
是否可以在 VB .NET 中执行一行 if 语句?如果是这样,如何?
回答by beach
Use IF().
使用 IF()。
It is a short-circuiting ternary operator.
它是一个短路三元运算符。
Dim Result = IF(expression,<true return>,<false return>)
SEE ALSO:
也可以看看:
回答by Quintin Robinson
It's actually pretty simple..
其实很简单。。
If CONDITION Then ..INSERT CODE HERE..
回答by xpda
At the risk of causing some cringing by purests and c# programmers, you can use multiple statements and else in a one-line if statement in VB. In this example, y ends up 3 and not 7.
冒着引起最纯粹的程序员和 c# 程序员一些畏缩的风险,您可以在 VB 中的单行 if 语句中使用多个语句和 else。在这个例子中,y 最终是 3 而不是 7。
i = 1
If i = 1 Then x = 3 : y = 3 Else x = 7 : y = 7
回答by Fluffy Sebbert
Don't know why people haven't posted this yet...
不知道为什么人们还没有发布这个......
Single line
单线
Syntax:
句法:
If (condition) Then (do this)
Example:
例子:
If flag = true Then i = 1
Multiple ElseIf's
多个 ElseIf
Syntax:
句法:
If (condition) Then : (do this)
ElseIf (condition2) Then : (do this)
Else : (do this)
End If
OR
或者
If (condition) Then : (do this) : ElseIf (condition2) Then : (do this) : Else : (do this) : End If
Multiple operations
多项操作
Syntax:
句法:
If (condition) Then : (do this) : (and this) : End If
Hope this will help someone.
希望这会帮助某人。
回答by Dmitriy Zhukov
Or
或者
IIf(CONDITION, TRUE_ACTION, FALSE_ACTION)
回答by Anton Gogolev
Just add Then
:
只需添加Then
:
If A = 1 Then A = 2
or:
或者:
If A = 1 Then _
A = 2
回答by nora
One Line 'If Statement'
一行“If 语句”
Easier than you think, noticed no-one has put what I've got yet, so I'll throw in my 2-cents.
比你想象的要容易,注意到没有人把我得到的东西放进去,所以我会投入我的 2 美分。
In my testing you don't need the continuation? semi-colon
, you can do without, also you can do it without the End If
.
在我的测试中continuation? semi-colon
,您不需要 ,也可以没有,也可以没有End If
.
<C#> = Condition.
<C#> = Condition.
<R#> = True Return.
<R#> = True Return.
<E> = Else Return.
<E> = Else Return.
Single Condition
单一条件
If <C1> Then <R1> Else <E>
Multiple Conditions
多重条件
If <C1> Then <R1> Else If <C2> Then <R2> Else <E>
Infinite? Conditions
无限的?状况
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
-Not really sure how to format this to make it more readable, so if someone could offer a edit, please do-
-不太确定如何格式化它以使其更具可读性,所以如果有人可以提供编辑,请做-
回答by S.Ozan
If (X1= 1) Then : Val1= "Yes" : Else : Val1= "Not" : End If
回答by Jon Limjap
You can use the IIf function too:
您也可以使用 IIf 函数:
CheckIt = IIf(TestMe > 1000, "Large", "Small")
回答by ravarador
If (condition, condition_is_true, condition_is_false)
It will look like this in longer version:
在更长的版本中它看起来像这样:
If (condition_is_true) Then
Else (condition_is_false)
End If