VB.NET覚書

VB.NETの覚書
[個人メモ] [仕事メモ] [アイデア] [Ruby]
[TIPS] [対応表] [コメント書き方] [.NET Tips] [VB中学校] [VB.NET の入門サイト] [VB.NET コーディング標準 ] [VBレスキュー] [NonSoft]

2016-04-27

構造体 Structure

クラスを作るほどではないデータに便利

arrylistと組み合わせると便利

Public Class Form1

    Private Structure Person
        Public Name As String
        Public Age As Integer
        Public Sub setPerson(ByVal n As String, ByVal a As Integer)
            Name = n
            Age = a
        End Sub
    End Structure

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        Dim list As New ArrayList

        Dim p As Person = New Person

        p.setPerson("John", 15)
        list.Add(p)

        For Each i As Person In list
            MsgBox(i.Name)
        Next

    End Sub
End Class


デリゲートサンプル

https://msdn.microsoft.com/ja-jp/library/ms172880(v=vs.100).aspx

Public Class Form1

    Delegate Function MathOperator(ByVal x As Double, ByVal y As Double) As Double

    Function SubtractNumnber(ByVal x As Double, ByVal y As Double) As Double
        Return x - y
    End Function
    Function AddNumber(ByVal x As Double, ByVal y As Double) As Double
        Return (x + y)
    End Function

    Sub DelegateTest(ByVal x As Double, ByVal op As MathOperator, ByVal y As Double)
        Dim ret As Double

        ret = op.Invoke(x, y)
        MsgBox(ret)

    End Sub


    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        DelegateTest(5, AddressOf AddNumber, 3)
        DelegateTest(9, AddressOf SubtractNumnber, 3)

    End Sub
End Class

http://indori.blog32.fc2.com/blog-entry-1742.html

'----------------------------------------------------
' 
'  ラムダ式
'  
'----------------------------------------------------
Imports System
Class Sample

    '引数が一つのデリゲート
    Delegate Function One(ByVal value As Integer) As String

    Shared Sub Main()

        '変換関数の例
        Dim convert As One = Function(x) (x.ToString())
        Console.WriteLine(convert(1))

        '通常はこちらを使用
        Dim toStr As Func(Of Integer, String) = Function(x) (x.ToString())
        Console.WriteLine(toStr(1))

        '終了
        Console.ReadLine()

    End Sub

End Class

このサンプルはラムダ式の本質がわかりやすいように、自分でデリゲートを定義していますが、通常はライブラリに用意されているSystem.Funcを使用します。

Public Class Form1

    Delegate Function MathOperator(ByVal x As Double, ByVal y As Double) As Double

    Function SubtractNumnber(ByVal x As Double, ByVal y As Double) As Double
        Return x - y
    End Function
    Function AddNumber(ByVal x As Double, ByVal y As Double) As Double
        Return (x + y)
    End Function

    Sub DelegateTest(ByVal x As Double, ByVal op As MathOperator, ByVal y As Double)
        Dim ret As Double

        ret = op.Invoke(x, y)
        MsgBox(ret)

    End Sub

    Dim mul As MathOperator = Function(x As Double, y As Double) x * y



    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        DelegateTest(5, AddressOf AddNumber, 3)
        DelegateTest(9, AddressOf SubtractNumnber, 3)
        DelegateTest(9, mul, 3)

    End Sub
End Class
トラックバック - http://sub.g.hatena.ne.jp/garyo/20160427