2018年1月26日金曜日

JS functionの引数にfunctionを渡す使い方

1)コードを簡潔に記述できる
function putYourHeadInTheSand(otherFunc) {
    try{
         otherFunc();
    } catch(e) { } // ignore the error
}

....

putYourHeadInTheSand(function(){
    // do something here
});
putYourHeadInTheSand(function(){
    // do something else
});

2)callback、非同期になる
Lets say you load some data somehow. Rather than locking up the system waiting for it to load, you can load it in the background, and do something with the result when it arrives.
function loadStuff(callback) {
    // Go off and make an XHR or a web worker or somehow generate some data
    var data = ...;
    callback(data);
}

loadStuff(function(data){
    alert('Now we have the data');
});

3)Lazy loading
まだ理解できていない

参考:https://stackoverflow.com/questions/2824393/javascript-function-as-a-parameter-to-another-function

C# (net framework 4.5以降) zip fileを扱う

using System;
using System.IO;
using System.IO.Compression; //このLibを使う

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string startPath = @"c:\example\start";
            string zipPath = @"c:\example\result.zip";
            string extractPath = @"c:\example\extract";

            ZipFile.CreateFromDirectory(startPath, zipPath);

            ZipFile.ExtractToDirectory(zipPath, extractPath);
        }
    }
}

ちなみに、「@」を利用するとエスケープを行わずに「/」を文字列に含められるので、パスを指定したい場合などに可読性が高く、便利に使える。

2018年1月24日水曜日

C# delegate

A delegate is like a pointer to a function. It is a reference type data type and it holds the reference of a method.
delegate は代表の意味である。定義と同じ戻り値と引数を持つMethodを代入できる。

class Program
{
    // declare delegate
    public delegate void Print(int value);

    static void Main(string[] args)
    {
        // Print delegate points to PrintNumber
        Print printDel = PrintNumber;
         
        printDel(100000);
        printDel(200);

        // Print delegate points to PrintMoney
        printDel = PrintMoney;

        printDel(10000);
        printDel(200);
    }

    public static void PrintNumber(int num)
    {
        Console.WriteLine("Number: {0,-12:N0}",num);
    }

    public static void PrintMoney(int money)
    {
        Console.WriteLine("Money: {0:C}", money);
    }
}

A method can have a parameter of a delegate type and can invoke the delegate parameter.

public static void PrintHelper(Print delegateFunc, int numToPrint)
{
    delegateFunc(numToPrint);
}

参考:http://www.tutorialsteacher.com/csharp/csharp-delegates

2018年1月12日金曜日

vba Variantのnullとempty

値 Empty は、初期化されていない (初期値が代入されていない) Variant 変数を示します。
Null は、 Variant 変数に意図的に有効なデータが格納されていないことを示します。