ASP.NET Razor - VB Mantık Koşulları
Programlama Mantığı: Koşullara göre kodu yürütün.
Eğer Koşul
VB, koşullara göre kod yürütmenize izin verir.
Bir koşulu test etmek için if ifadesini kullanırsınız . if ifadesi, testinize bağlı olarak doğru veya yanlış döndürür:
- if ifadesi bir kod bloğu başlatır
- Koşul if ile o zaman arasında yazılır
- if ... then ve end if arasındaki kod, test doğruysa yürütülür
Örnek
@Code
Dim
price=50
End Code
<html>
<body>
@If price>30
Then
@<p>The price is too high.</p>
End
If
</body>
</html>
Başka Durum
Bir if ifadesi, else koşulu içerebilir .
else koşulu, koşulun yanlış olması durumunda yürütülecek kodu tanımlar.
Örnek
@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>
</html>
Not: Yukarıdaki örnekte, ilk koşul doğruysa yürütülecektir. Diğer koşulu "diğer her şeyi" kapsar.
ElseIf Koşul
Birden çok koşul, else if koşuluyla test edilebilir :
Örnek
@Code
Dim
price=25
End Code
<html>
<body>
@If price>=30 Then
@<p>The price is
high.</p>
ElseIf price>20 And price<30 then
@<p>The price is OK.</p>
Else
@<p>The price is low.</p>
End If
</body>
</html>
Yukarıdaki örnekte, ilk koşul doğruysa yürütülecektir.
Değilse, bir sonraki koşul doğruysa, bu koşul yürütülür.
Koşullar varsa, herhangi bir sayıda başkasına sahip olabilirsiniz.
if veya else if koşullarından hiçbiri doğru değilse, son else bloğu (koşulsuz) "diğer her şeyi" kapsar.
Koşulları Seç
Bir dizi bireysel koşulu test etmek için bir seçme bloğu kullanılabilir:
Örnek
@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>
"Vaka Seç" ifadesini test değeri (gün) takip eder. Her bir test koşulunun bir vaka değeri ve herhangi bir sayıda kod satırı vardır. Test değeri durum değeriyle eşleşirse kod satırları yürütülür.
Bir seçim bloğu, diğer durumlardan hiçbiri doğru değilse çalışan "diğer her şey" için varsayılan bir duruma (Diğer Durum) sahip olabilir.