VB.Net/Language Basics/If

Материал из VB Эксперт
Версия от 12:42, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

If ElseIf Else End If Demo

Imports System
Public Class MainClass
    Shared Sub Main()
        "Declare and set a variable
        Dim intNumber As Integer = 27
        If intNumber = 1000 Then
            System.Console.WriteLine(""intNumber" is, indeed, 1000!")
        ElseIf intNumber = 27 Then
            System.Console.WriteLine(""intNumber" is 27!")
        Else
            System.Console.WriteLine(""intNumber" is not 1000!")
        End If
    End Sub
End Class


If Then Else Demo

Imports System
Public Class MainClass
    Shared Sub Main()
        "Declare variables
        Dim strName1 As String, strName2 As String
        "Get the names
        strName1 = "Sydney"
        strName2 = "Vancouver"
        "Is one of the names Sydney?
        If strName1 = "Sydney" Or strName2 = "Sydney" Then
            System.Console.WriteLine("One of the names is Sydney.")
        Else
            System.Console.WriteLine("Neither of the names is Sydney.")
        End If
    End Sub
End Class


If then else if

Imports System
public class MainClass
    Shared 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)
       Else
          Console.WriteLine( _
             "Nope, ValueOne: {0} is NOT larger than valueTwo: {1}", _
              valueOne, valueTwo)
       End If
    End Sub
End Class


If with Else

Imports System
Public Class MainClass
    Shared Sub Main(ByVal args As String())
       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)
       Else
          Console.WriteLine("Nope, ValueOne: {0} is NOT larger than valueTwo: {1}", valueOne, valueTwo)
       End If
    End Sub

End Class


Nested If

Imports System
public class MainClass
    Shared Sub Main()
       Dim temp As Integer = 32
       If temp <= 32 Then
          Console.WriteLine("temp <= 32")
          If temp = 32 Then
             Console.WriteLine("temp = 32")
          Else
             Console.WriteLine("Temp: {0}", temp)
          End If
       End If
    End Sub
End Class


Simple If Then

Imports System
Public Class MainClass
    Shared Sub Main()
        Dim intNumber As Integer = 27
        If intNumber = 27 Then
            System.Console.WriteLine(""intNumber" is, indeed, 27!")
        End If
    End Sub
End Class


Use function in if statement

Imports System
public class MainClass
    Shared Sub Main()
         Dim x As Integer = 7
         If IsSmaller(x, 8) OrElse IsBigger(x, 12) Then
             Console.WriteLine("x < 8 OrElse x > 12")
         Else
             Console.WriteLine("Not True that x < 8 OrElse x > 12")
         End If

    End Sub
    Shared Function IsBigger(ByVal firstVal As Integer,ByVal secondVal As Integer) _
         As Boolean
         If firstVal > secondVal Then
             Return True
         Else
             Return False
         End If
     End Function
    Shared Function IsSmaller(ByVal firstVal As Integer,ByVal secondVal As Integer) _
         As Boolean
         If firstVal < secondVal Then
             Return True
         Else
             Return False
         End If
     End Function
End Class