LoginSignup
1
1

More than 3 years have passed since last update.

VBの無名関数(デリゲート)サンプルメモ

Last updated at Posted at 2016-11-04

概要

またもやクソ投稿でもうしわけない。
1つの関数内で使い回したいちっちゃなロジックが欲しいなと思ったとき、外部にPrivateとかで関数を作ると1つのクラス内でPrivateが増殖しちゃうので、Javascriptみたいに無名関数(デリゲート)を作ります。
その時のメモ的なものです。

サンプル

VB

コンソールアプリで下記を作成。
MSDN Func(Of T, TResult) Delegate

Module Module1

    Sub Main()
        Dim list As New List(Of String)

        Dim TestFunc As Func(Of List(Of String), List(Of String)) = _
         (Function(p_ As List(Of String)) As List(Of String)
              Dim a As New List(Of String)

              For Each p As String In p_
                  list.Add(p)
                  a.Add(p & "!")
              Next

              Return a
          End Function)

        Console.WriteLine("*************")
        For Each a As String In list
            Console.WriteLine(a)
        Next

        Console.WriteLine("setTest")
        For Each a As String In TestFunc(New List(Of String) From {"a", "b", "c"})
            Console.WriteLine(a)
        Next

        Console.WriteLine("dim list")
        For Each a As String In list
            Console.WriteLine(a)
        Next

    End Sub

End Module

実行結果
(知らない人が身近にいたので、、、コンソールは実行するとEnd Subでプロセス自体終了しますが、Ctrl + F5すれば止まります。)

result.png

js

jQuery.eachとかやっちゃったので、jQueryのサイトとかでコンソール開いてコピペして下さい。

var list = [];
var TestFunc = function(p_){
    var a = [];

    jQuery.each(p_, function(i, v){
        list.push(v);
        a.push(v + '!');
    });

    return a;
};

console.log('*************');
console.log(list);

console.log('setTest');
console.log(TestFunc(['a', 'b', 'c']));

console.log('list');
console.log(list);

実行結果

result.png

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1