「VB.NET」IsNothing関数で変数Null判定をする

書式
If 変数名 Is nOthing Then
処理コード1
Else
処理コード2
End If

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Module Module1
Public Sub Main()
'変数定義
Dim strA = Nothing
Dim strB = "google"
'Nothing判定
If strA Is Nothing Then
Console.WriteLine("変数名strAはNothingです。")
Else
Console.WriteLine("変数名strAはNothingではありません。")
End If
'Nothing判定
If strB Is Nothing Then
Console.WriteLine("変数名strBはNothingです。")
Else
Console.WriteLine("変数名strBはNothingではありません。")
End If
Console.ReadKey()
End Sub
End Module
Module Module1 Public Sub Main() '変数定義 Dim strA = Nothing Dim strB = "google" 'Nothing判定 If strA Is Nothing Then Console.WriteLine("変数名strAはNothingです。") Else Console.WriteLine("変数名strAはNothingではありません。") End If 'Nothing判定 If strB Is Nothing Then Console.WriteLine("変数名strBはNothingです。") Else Console.WriteLine("変数名strBはNothingではありません。") End If Console.ReadKey() End Sub End Module
Module Module1

    Public Sub Main()

        '変数定義
        Dim strA = Nothing
        Dim strB = "google"
        'Nothing判定
        If strA Is Nothing Then
            Console.WriteLine("変数名strAはNothingです。")
        Else
            Console.WriteLine("変数名strAはNothingではありません。")
        End If

        'Nothing判定
        If strB Is Nothing Then
            Console.WriteLine("変数名strBはNothingです。")
        Else
            Console.WriteLine("変数名strBはNothingではありません。")
        End If

        Console.ReadKey()
    End Sub

End Module

実行結果
変数名strAはNothingです。
変数名strBはNothingではありません。

VB.net

Posted by arkgame