泥庭

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

2013年6月29日

超LINQ入門

Filed under: .NET, 勉強会 — タグ: , , — yone64 @ 4:44 午後

LINQ勉強会で発表してきました。
2時間のハンズオンだったんですが、ただただ疲れた。

ネットワーク環境がなかったので、結構見ながら移してもらう状況が多かったのがちょっと残念でしたが、がんばったよ。

以下資料です。

なお、ソースコードはGitHubにあげてます。

ハンズオンの順序でコミットしてるので過去からログを参照していただければと思います。

2013年6月10日

System.Charの比較

Filed under: .NET — タグ: , — yone64 @ 11:26 午後

最近、VB.NETやってます。で、System.Charの扱いがC#とVB.NETで結構異なってビックリしたので、まとめ。
なお、すべてOption Strict Onでの結果です。

StringとCharの暗黙的変換。

C#erからすると大変気持ち悪い構文ですが、下記構文はコンパイルが通り、Equalが表示されます。

Dim chr As Char = "a"c
Dim str As String = "a"

If (chr = str) Then
    Console.WriteLine("Equal")
Else
    Console.WriteLine("Not Equal")
End If

当然ながら、C#で同等の記述の下記は、コンパイルエラーとなります。

char chr = 'a';
string str = "a";

if (chr == str)
{
    Console.WriteLine("Equal");
}
else
{
    Console.WriteLine("Not Equal");
}

これは、何が起こっているか気になりますね。って事で、ILSpyの出番です。

Dim chr As Char = "a"
Dim str As String = "a"
Dim flag As Boolean = Operators.CompareString(Conversions.ToString(chr), str, False) = 0
If flag Then
    Console.WriteLine("Equal")
Else
    Console.WriteLine("Not Equal")
End If

文字列比較のためにOperators.CompareStringメソッドが呼ばれてます。なおかつConversions.ToStringメソッドでCharからStringへの変換が暗黙的に行われているようです。

IntegerとCharの変換

C#でのchar型は数値として扱うことが可能で、int等の数値型へ暗黙的に変換が行えます。

char c = 'a';
int i = c;
Console.WriteLine(i);   // 97 が表示される

しかし、VB.NETではあくまでも文字としてしか扱えないようです。

Dim c As Char = "a"c
Dim i As Integer = c    ' コンパイルエラー

Console.WriteLine(i)

きっとVB.NETやってる人にとっては常識なんだろうな。

2013年2月14日

C#erのためのVB.NET覚書

Filed under: .NET, 未分類 — タグ: , — yone64 @ 4:07 午後

配列編です。いろいろ気持ち悪い。

Module Module1

    Sub Main()

    End Sub

    Sub Array1()
        ' 1次元配列の宣言 括弧はどっちでも同じ
        Dim arrA As String() = Nothing
        Dim arrB() As String = Nothing

        ' 配列を決まった長さで初期化する方法 ※すべて要素数6で初期化
        Dim arrC As String() = New String(5) {}
        Dim arrD() As String = New String(5) {}
        ' 変数名に括弧が付く場合は、そのまま初期化が可能
        Dim arrE(5) As String
        ' どちらも型推論できるけど、括弧の有無が気持ち悪い
        Dim arrF = New String(5) {}
        Dim arrG() = New String(5) {}

        ' 配列を要素で初期化する
        Dim arrH As String() = New String() {"1", "2", "3"}
        Dim arrI As String() = {"1", "2", "3"}
        Dim arrJ = {"1", "2", "3"}
        Dim arrK() As String = New String() {"1", "2", "3"}
        Dim arrL() As String = {"1", "2", "3"}
        Dim arrM() = {"1", "2", "3"}

        'いろんな代入
        arrA = New String(1) {}         '要素数2
        arrA = New String(0) {}         '要素数1
        arrA = New String(-1) {}        '要素数0 (キモッ
        arrA = New String() {}          '要素数0
        arrB = New String(1) {"1", "2"}
        arrB = New String() {"1", "2"}
        arrB = {"1", "2"}
    End Sub


    Sub Array2()
        ' 2次元配列の宣言
        Dim arrA As String(,) = Nothing
        Dim arrB(,) As String = Nothing

        ' 初期化
        Dim arrC As String(,) = New String(1, 2) {}
        Dim arrD = New String(1, 2) {}
        Dim arrE(,) As String = New String(1, 2) {}
        Dim arrF(,) = New String(1, 2) {}
        Dim arrG(1, 2) As String


        Dim arrH As String(,) = New String(,) {{"1", "2", "3"}, {"a", "b", "c"}}
        'これは2次元配列扱いだそうです。
        Dim arrI As String(,) = {{"1", "2", "3"}, {"a", "b", "c"}}
        Dim arrJ = {{"1", "2", "3"}, {"a", "b", "c"}}

    End Sub


    Sub ArrayOfArray()
        ' 多段配列の宣言
        Dim arrA As String()() = Nothing
        Dim arrB()() As String = Nothing

        ' 初期化
        Dim arrC As String()() = New String(1)() {}
        Dim arrD = New String(1)() {}
        Dim arrE()() = New String(1)() {}
        Dim arrF(1)() As String

        ' 要素で初期化 
        ' New String()は省略できない 2次元配列との兼ね合い?
        Dim arrH As String()() = New String()() {New String() {"1", "2"}, New String() {"a", "b", "c"}}
        Dim arrI = {New String() {"1", "2"}, New String() {"a", "b", "c"}}


    End Sub
End Module

 

C#erのためのVB.NET 覚書

Filed under: .NET — タグ: , — yone64 @ 1:51 午後

VB.NETでは、「’」シングルクォートがコメント扱いなので、Char型の定義がC#と異なっています。
cというサフィックスをつけましょう。

Module Module1

    Sub Main()

        ' 型推論が効く
        Dim id = 1
        Dim str = "abc"

        ' System.Int32
        Console.WriteLine(id.GetType)
        ' System.String
        Console.WriteLine(str.GetType)

        ' 通常のChar型の定義
        Dim ch As Char = "C"

        ' これだとStringに推論されてしまう
        Dim s1 = "C"

        ' Charに推論するためには、サフィックスをつける
        Dim c = "c"c

    End Sub

End Module

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