VB.Net Tutorial/Statements/If

Материал из VB Эксперт
Перейти к: навигация, поиск

ElseIf

<source lang="vbnet">public class Test

  public Shared Sub Main
       Dim intScore As Integer
       Dim strResult As String
       intScore = 78
       If intScore < 50 Then
           strResult = "Failed."
       ElseIf intScore < 75 Then
           strResult = "Pass."
       ElseIf intScore < 90 Then
           strResult = "Very Good."
       Else
           strResult = "Excellent."
       End If
       Console.WriteLine(strResult)
  End Sub

End class</source>

Very Good.

Else If (Not ELSEIF)

<source lang="vbnet">Imports System Public Class Test

    Shared Sub Main()
       Dim dtCurrent As System.DateTime
       Dim iHour As Integer
       dtCurrent = dtCurrent.Now()
       iHour = dtCurrent.Hour
       If (iHour < 12) Then
           Console.Writeline("Good Morning!")
       Else If (iHour >= 12) And (iHour < 18) Then
              Console.WriteLine("Good Afternoon!")
       Else
           Console.WriteLine("Good Evening!")
       End If
   End Sub

End Class</source>

Very Good.

If Short Circuiting with ANDALSO

<source lang="vbnet">Public Class ShortCircuiting

    Shared Sub Main()
        If Test("Left") ANDALSO Test("Right") Then
            "do something
        End If
    End Sub

    Shared Function Test(sInput As String) As Boolean
        System.Console.WriteLine(sInput)
        Test = FALSE
    End Function

End Class</source>

Left

If statement Short Circuiting

<source lang="vbnet">Public Class ShortCircuiting

    Shared Sub Main()
        If Test("Left") AND Test("Right") Then
            "do something
        End If
    End Sub

    Shared Function Test(sInput As String) As Boolean
        System.Console.WriteLine(sInput)
        Test = FALSE
    End Function

End Class</source>

Left
Right

If with And

<source lang="vbnet">Imports System Public Class Test

    Shared Sub Main()
       Dim dtCurrent As System.DateTime
       Dim iHour As Integer
       dtCurrent = dtCurrent.Now()
       iHour = dtCurrent.Hour
       If (iHour < 12) Then
           Console.Writeline("Good Morning!")
       ElseIf (iHour >= 12) And (iHour < 18) Then
           Console.WriteLine("Good Afternoon!")
       Else
           Console.WriteLine("Good Evening!")
       End If
   End Sub

End Class</source>

Good Afternoon!

Nested if statement

<source lang="vbnet">Option Strict On

Imports System
Module Module1
   Sub Main( )
      Dim temp As Integer = 32
      If temp <= 32 Then
         If temp = 32 Then
            Console.WriteLine("temp = 32")
         Else
            Console.WriteLine("Temp: {0}", temp)
         End If
      End If 
   End Sub 
End Module</source>
temp = 32

Single line vs Multi-line If statement

<source lang="vbnet">public class Test

  public Shared Sub Main
       Dim intScore As Integer
       intScore = 23
       "Single line If statement.
       If intScore < 50 Then Console.WriteLine("Failed.") : Console.WriteLine("Please try again.")
       "Multi-line If statement.
       If intScore < 50 Then
           Console.WriteLine("Failed.")
           Console.WriteLine("Please try again.")
       End If
  End Sub

End class</source>

Failed.
Please try again.
Failed.
Please try again.

Use ElseIf

<source lang="vbnet">Option Strict On

Imports System
Module Module1
   Sub Main( )
      Dim temp As Integer = -32
      If temp > 32 Then
      ElseIf temp = 32 Then
         Console.WriteLine("= 32")
      ElseIf temp > 0 Then
      ElseIf temp = 0 Then
         Console.WriteLine(" = 0")
      Else
         Console.WriteLine("Else")
      End If
   End Sub "Main
End Module</source>
Else

Use If statement in a For statement

<source lang="vbnet">public class Test

  public Shared Sub Main
       Dim intCounter As Integer
       Dim strString As String = "some silly string"
       For intCounter = 0 To strString.Length - 1
           If strString(intCounter) = "s" Then
               strString = strString.Remove(intCounter, 1)
               strString = strString.Insert(intCounter, "S")
           End If
       Next intCounter
       Console.WriteLine(strString)
  End Sub

End class</source>

Some Silly String

Use if statement to compare Integer

<source lang="vbnet">Option Strict On

Imports System
Module Module1
   Sub Main( )
      Dim valueOne As Integer = 10
      Dim valueTwo As Integer = 20
      Dim valueThree As Integer = 30
      If valueOne > valueTwo Then
         Console.WriteLine("ValueOne: {0} larger than ValueTwo: {1}", valueOne, valueTwo)
      End If
      If valueThree > valueTwo Then
         Console.WriteLine("ValueThree: {0} larger than ValueTwo: {1}",valueThree, valueTwo)
      End If
      If valueTwo > 15 Then 
           Console.WriteLine("Yes it is")
      End If
   End Sub "Main
End Module</source>
ValueThree: 30 larger than ValueTwo: 20
Yes it is