VB.NETの演算子について - サンプル

2011/08/16 15:04Update
TAGS: VB.NET | 演算子 | TypeOf | And | AndAlso | Or | OrElse | Not | Is

VB.NETの演算子一覧とそのサンプル。

■VB.NETの演算子一覧

◇TypeOf    
If TypeOf value Is String Then



◇条件 And/AndAlso
boolean-expression1 AndAlso boolean-expression2

◇And(論理積)
True And False


◇条件 Or
boolean-expression1 OrElse boolean-expression2

◇Or(論理和)
True Or False


◇条件 等しい(=)
a = b

◇条件 等しくない(!=)
a <> b

◇文字列の連結
string1 & test
var1 &= var2

◇文字列の空でない判断    
If str <>  Then
        If str <> String.Empty Then
        If str IsNot Nothing And Not s.Equals() Then
        If Not String.IsNullOrEmpty(str) Then

◇Not演算子
Not (a > b)

◇オブジェクトの比較演算子
a Is b
Not a Is b
a IsNot b


◇And(ビット処理)演算子
1 And 2


◇Or(ビット処理)演算子
1 Or 2


◇除算の剰余演算子
10 Mod 5


■演算子サンプル
Module Module1
    Sub Main()
        If TypeOf "test" Is String Then
            Console.WriteLine("1)TypeOf 'test' Is String")
        End If

        Dim b1 As Boolean = True
        Dim b2 As Boolean = True

        If (b1 AndAlso b2) Then
            Console.WriteLine("2)b1=true and b2=true")
        End If

        If (b1 And b2) Then
            Console.WriteLine("3)b1=true and b2=true")
        End If

        b2 = False
        If (b1 OrElse b2) Then
            Console.WriteLine("4)b1=true or b2=true")
        End If

        If (b1 Or b2) Then
            Console.WriteLine("5)b1=true or b2=true")
        End If

        If (b1 = b2) Then       ' if 1= 2 then
            Console.WriteLine("6)b1=b2")       '出力されません
        End If

        If (b1 <> b2) Then       ' if 1 <> 2 then
            Console.WriteLine("7)b1<>b2")
        End If

        Dim str As String = ""
        str &= "hello"
        str = str & " world"


        Dim a As Integer = 2
        Dim b As Integer = 1
        If (Not (a < b)) Then
            Console.WriteLine("8)a>=b")
        End If

        Dim o1 As Object = "a"
        Dim o2 As Object = o1
        If (o1 Is o2) Then
            Console.WriteLine("9)o1 is o2")
        End If

        If o1 IsNot Nothing Then
            Console.WriteLine("10)o1 is not nothing")
        End If

        o1 = Nothing
        If (o1 Is Nothing) Then
            Console.WriteLine("11)o1 is nothing")
        End If

        If TypeOf (o1) Is String Then
            Console.WriteLine("12)typeof o1 is String")
        End If

        Dim x As Integer
        x = 1 And 2                 '01 And 10 = 00(2進数) = 0(10進数)
        Console.WriteLine("13)" & x)

        x = 1 Or 2                  '01 Or 10 = 11(2進数) = 3(10進数)
        Console.WriteLine("14)" & x)               

        Console.WriteLine("15)" & (10 Mod 5))     '0

        Console.Read()
    End Sub

End Module


■出力結果
1)TypeOf 'test' Is String
2)b1=true and b2=true
3)b1=true and b2=true
4)b1=true or b2=true
5)b1=true or b2=true
7)b1<>b2
8)a>=b
9)o1 is o2
10)o1 is not nothing
11)o1 is nothing
13)0
14)3
15)0

有关作者
Syboos.jp編集長AJavaやオープンソース情報の執筆、Webサイトの開発や運営全般の業務に携わる。

Sponsored Link


Comments

用户名 (required)

Email (will not be published) (required)

URL

Evaluation