泥庭

2014年2月10日

C#erのためのVB.NET覚書(まとめ

Filed under: 未分類 — タグ: — yone64 @ 5:31 午後

まとまるかどうかはさておき、VB.NETでのお仕事がひと段落したので、個人的に気になった点ベスト5をば。

第5位 Char

C#ではCharは数値状態で扱えますが、VB.NETでは一文字として扱います。
なお、Charの配列はStringと暗黙的に変換します。

↓過去エントリー
https://yone64.wordpress.com/2013/02/14/c%E3%81%AE%E3%81%9F%E3%82%81%E3%81%AEvb-net-%E8%A6%9A%E6%9B%B8/

第4位 配列

これはVB6時代の負の遺産を引きずってる筆頭だと思うのですが、配列の宣言が異なりますよ。
まぁ、要素数を指定して配列を定義することはあまりないですけどね。

↓過去エントリー
https://yone64.wordpress.com/2013/02/14/cer%E3%81%AE%E3%81%9F%E3%82%81%E3%81%AEvb-net%E8%A6%9A%E6%9B%B8/

第3位 自動実装プロパティー

VB.NETでは自動実装プロパティーのバッキングフィールドにアクセスできるんですね。
Propertyでは、「_」から始まるフィールドが、Eventでは「Event」が末尾についたデリゲートが自動生成され、プログラム中からアクセスが可能です。
# あっ、Eventは自動実装プロパティーじゃないや

Public Class Class1

    Public Property Property1 As String

    Public Event MyEvent As EventHandler

    Public Sub Method1()

        Me.Property1 = "aa"
        Me._Property1 = "bb"

        RaiseEvent MyEvent(Me, EventArgs.Empty)
        Dim a = MyEventEvent

    End Sub

End Class

第2位 Nothing

NothingはC#でいうところのdefault(T)に当たります。なので、

Dim i As Integer = Nothing

みたいな宣言も可能です。あと、難しいのは比較。= Nothingだったり、Is Nothingだったりですね。空文字列=NothingがTrueになるのはよくわかりません。

Module Module1
    Sub Main()

        Dim s1 As String = Nothing
        Dim s2 As String = String.Empty
        Dim s3 As String = "abc"

        If s1 Is Nothing Then
            Console.WriteLine("s1 Is Nothing")
        Else
            Console.WriteLine("s1 IsNot Nothing")
        End If

        If s2 Is Nothing Then
            Console.WriteLine("s2 Is Nothing")
        Else
            Console.WriteLine("s2 IsNot Nothing")
        End If
        If s3 Is Nothing Then
            Console.WriteLine("s3 Is Nothing")
        Else
            Console.WriteLine("s3 IsNot Nothing")
        End If

        Console.WriteLine("--")

        If s1 = Nothing Then
            Console.WriteLine("s1 = Nothing")
        Else
            Console.WriteLine("s1 <> Nothing")
        End If
        If s2 = Nothing Then
            Console.WriteLine("s2 = Nothing")
        Else
            Console.WriteLine("s2 <> Nothing")
        End If
        If s3 = Nothing Then
            Console.WriteLine("s3 = Nothing")
        Else
            Console.WriteLine("s3 <> Nothing")
        End If

        Console.WriteLine("--")

        Dim ni1 As Integer? = Nothing
        Dim ni2 As Integer? = 0
        Dim ni3 As Integer? = 123

        If ni1 Is Nothing Then
            Console.WriteLine("ni1 Is Nothing")
        Else
            Console.WriteLine("ni1 IsNot Nothing")
        End If
        If ni2 Is Nothing Then
            Console.WriteLine("ni2 Is Nothing")
        Else
            Console.WriteLine("ni2 IsNot Nothing")
        End If
        If ni3 Is Nothing Then
            Console.WriteLine("ni3 Is Nothing")
        Else
            Console.WriteLine("ni3 IsNot Nothing")
        End If

        Dim i1 As Integer = Nothing
        Dim i2 As Integer = 0
        Dim i3 As Integer = 123

        If i1 = Nothing Then
            Console.WriteLine("i1 = Nothing")
        Else
            Console.WriteLine("i1 <> Nothing")
        End If
        If i2 = Nothing Then
            Console.WriteLine("i2 = Nothing")
        Else
            Console.WriteLine("i2 <> Nothing")
        End If
        If i3 = Nothing Then
            Console.WriteLine("i3 = Nothing")
        Else
            Console.WriteLine("i3 <> Nothing")
        End If

    End Sub
End Module

結果

image

第1位 3値論理

SQLとかでよくあるやつですね。VB.NETではNull許可型同士の比較はBooleanではなく、Boolean?型になります。これ、Nullable型の時だけなんですよね。なぜ?って感じ。

Module Module1
    Sub Main()

        Dim ni1 As Integer? = Nothing
        Dim ni2 As Integer? = 0
        Dim ni3 As Integer? = 1

        Dim flag1 = (ni1 = ni2)

        Console.WriteLine(flag1 Is Nothing)

    End Sub
End Module

image

コメントする »

まだコメントはありません。

RSS feed for comments on this post. TrackBack URI

コメントを残す

WordPress.com で無料サイトやブログを作成.