ASP.NET Razor - VB 逻辑条件
プログラミング論理:条件に基づいてコードを実行する。
If 条件
VB では条件に基づいてコードを実行できます。
使用if 语句条件を判断します。判断結果に基づいて、if ステートメントは true または false を返します:
- if ステートメントはコードブロックを開始します
- 条件はifとthenの間に書きます
- 条件が真の場合、if ... then と end if の間のコードが実行されます。
实例
@Code
Dim price=50
End Code
<html>
<body>
@If price>30 Then
@<p>The price is too high.</p>
End If
</body>
</html>
Dim price=50
End Code
<html>
<body>
@If price>30 Then
@<p>The price is too high.</p>
End If
</body>
</html>
运行实例 »
Else 条件
if文は~を含むことができるelse 条件。
else 条件は、条件が偽の場合に実行されるコードを定義します。
实例
@Code
Dim price=20
End Code
<html>
<body>
@if price>30 then
@<p>The price is too high.</p>
Else
@<p>The price is OK.</p>
End If
</body>
</htmlV>
Dim price=20
End Code
<html>
<body>
@if price>30 then
@<p>The price is too high.</p>
Else
@<p>The price is OK.</p>
End If
</body>
</htmlV>
运行实例 »
注释:上記の例では、最初の条件が真の場合、if ブロックのコードが実行されます。else 条件は、if 条件以外の「その他すべての場合」をカバーします。
ElseIf 条件
複数の条件判断を使用できますelseif 条件:
实例
@Code
Dim price=25
End Code
<html>
<body>
@If price>=30 Then
@<p>The price is high.</p>
ElseIf price>20 And price<30
@<p>The price is OK.</p>
Else
@<p>The price is low.</p>
End If
</body>
</html>
Dim price=25
End Code
<html>
<body>
@If price>=30 Then
@<p>The price is high.</p>
ElseIf price>20 And price<30
@<p>The price is OK.</p>
Else
@<p>The price is low.</p>
End If
</body>
</html>
运行实例 »
上記の例では、最初の条件が真の場合、if ブロックのコードが実行されます。
最初の条件が真でなく、2番目の条件が真の場合、elseif ブロックのコードが実行されます。
elseif 条件の数に制限はありません。
if と elseif の条件がすべて真でない場合、最後の else ブロック(条件なし)が「その他すべてのケース」をカバーします。
Select 条件
select 块いくつかの個別の条件をテストするために使用できます:
实例
@Code
Dim weekday=DateTime.Now.DayOfWeek
Dim day=weekday.ToString()
Dim message=""
End Code
<html>
<body>
@Select Case day
Case "Monday"
message="This is the first weekday."
Case "Thursday"
message="Only one day before weekend."
Case "Friday"
message="Tomorrow is weekend!"
Case Else
message="Today is " & day
End Select
<p>@message</p>
</body>
</html>
Dim weekday=DateTime.Now.DayOfWeek
Dim day=weekday.ToString()
Dim message=""
End Code
<html>
<body>
@Select Case day
Case "Monday"
message="This is the first weekday."
Case "Thursday"
message="Only one day before weekend."
Case "Friday"
message="Tomorrow is weekend!"
Case Else
message="Today is " & day
End Select
<p>@message</p>
</body>
</html>
运行实例 »
"Select Case" の直後にはテスト値(day)が続きます。個々のテスト条件にはそれぞれ case 値と任意の数のコード行があります。テスト値が case 値と一致する場合、対応するコード行が実行されます。
select ブロックにはデフォルトのケース(Case Else)があり、指定されたすべてのケースが一致しない場合に「その他すべてのケース」をカバーします。
其他扩展