VB.NETの演算子について - サンプル
2011/08/16 15:04Update
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
■演算子サンプル
■出力結果
◇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
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
Sponsored Link
Comments
- Relative Articles
- typeofによる 型(System.Type)の取得 - (2009/10/15 16:58)
- C# オブジェクトの型の判定 - (2009/11/11 16:22)
- VB.NET メール送信例 - (2011/08/31 13:34)
- VB.NETの異常処理 - Try ~ Catch ~ 構文 例 - (2011/08/10 15:07)
- VB.NETのHTTPダウンロード例 - (2011/08/10 15:23)
- VB.NETでのインスタンスについて - (2011/08/10 16:16)
- VB.NETの名前空間定義 - (2011/08/10 16:57)
- VB.NET クラスの定義 - (2011/08/10 17:47)
- VB.NET インタフェースの定義とその使用例 - (2011/08/11 11:35)
- VB.NET クラスのコンストラクタ - (2011/08/11 11:55)