2016-09-28 15 views
-6

関数呼び出しエラー。私はこれがコンソールウィンドウアプリケーションですCのイベントと代理人

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace delegatEvents 
{ 

    delegate int number(int i); 
    class Program 
    { 
     public static event number variable; 
     static int no = 5; 

     public static int addnumber(int p) 
     { 

      no += p; 
      return no; 
     } 
     public static int multiply(int m) 
     { 
      no *= m; 
      return no; 
     } 

     public static int getnum() 
     { 
      return no; 
     } 

     static void Main(string[] args) 
     { 
      number p; 

      variable +=new number(addnumber); 
      variable += new number(multiply); 

      variable(3); 
      Console.WriteLine(" addition number is : {0}", getnum()); 
      variable(2); 
      Console.WriteLine(" multiplication number is : {0}", getnum()); 

      Console.ReadLine(); 
     } 
    } 
} 

の下に私のコードは一緒に複数の関数を呼び出すしようとしています、私はプログラムを使用して加算や乗算を取得しようとしていますが、そのは動作していません。

+2

これにはイベントを使用することはほとんど意味がありません。両方のイベントを購読しているので、最初にaddが実行され、次に 'variable(n)'の呼び出しごとに乗算が行われます。 – CodeCaster

+2

ここをクリックしてください - [Events and Delegates Simplified](http://www.codeproject.com/Articles/4773/Events-and-Delegates-Simplified) –

+0

このように、デリゲート '' 'variable'''、最初にno = 3 + no(5 + 3 = 8)を追加し、no = 3 * no = 24を掛けます。次に、 '' variable(2); ''変数(3) '' ''最初にno = 2 + no = 26を追加し、no = 2 * no = 52を掛けます。それはなぜ出力24と52になるのですか? – tym32167

答えて

1

さて、私はそれがどのように機能するか説明しようとします。デリゲートはメソッドへのポインタのようなものです。

delegate int SomeDelegate(int i); 

public class Delegates 
{ 
    public SomeDelegate Delegate { get; set; } 

    public Delegates(SomeDelegate _delegate) 
    { 
     Delegate = _delegate; 
    } 

    public int InvokeDelegate(int i) 
    { 
     return Delegate(i); 
    } 
} 

上記のクラスでは、最初の行がデリゲート型宣言であることがわかります。私たちが見たい "メソッドパターン"について説明します。この場合、整数を返し、整数を受け入れるメソッドが必要です。

プロパティ "代理人"は、コンストラクタで割り当てた "メソッドポインタ"を格納するために宣言されています。

InvokeDelegateメソッドは、単にDelegateプロパティに格納されたメソッドを呼び出します。 .NET 3.5では、ActionとFuncのような「構文砂糖」をいくつか持っています。

  • アクション - 結果(void)のない単純な汎用代理人です。
  • Func - これはアクションとまったく同じですが、結果があります。以下の例があります:

    public class NewDelegates 
    { 
        public Action VoidDelegate { get; set; } 
        public Action<int> VoidDelegateWithIntParameter { get; set; } 
    
        public Func<int> NonVoidDelegate { get; set; } 
        public Func<int, int> NonVoidDelegateWithParameter { get; set; } 
    
        public NewDelegates(Action _voidDelegate, 
             Action<int> _voidDelegateWithParameter, 
             Func<int> _nonVoidDelegate, 
             Func<int, int> _nonVoidDelegateWithParameter) 
        { 
         VoidDelegate = _voidDelegate; 
         VoidDelegateWithIntParameter = voidDelegateWithParameter; 
    
         NonVoidDelegate = nonVoidDelegate; 
         NonVoidDelegateWithParameter = nonVoidDelegateWithParameter; 
        } 
    
        public void InvokeVoid() 
        { 
         VoidDelegate(); 
        } 
    
        public void InvokeVoidWithParams(int arg) 
        { 
         VoidDelegateWithIntParameter(arg); 
        } 
    
        public int InvokeNonVoid() 
        { 
         return NonVoidDelegate(); 
        } 
    
        public int InvokeNonVoidDelegateWithParameter(int arg) 
        { 
         return NonVoidDelegateWithParameter(arg); 
        } 
    } 
    

今の出来事について話しましょう。簡単に言えば、イベントは配列を委譲しています。イベントを呼び出すと、格納されているすべてのデリゲートが呼び出されます。以下に簡単な例があります:

public class Events 
{ 
    public event Action<int> OnValueChanged; 
    public int Value { get; set; } 

    public void ChangeValue(int i) 
    { 
     Value = i; 

     if(OnValueChanged != null) 
      OnValueChanged(Value); 
    } 
} 

上記のように、「サブスクライバ」がない場合、イベントはnullになる可能性があります。また、イベントは、それを拒否するクラスからのみ呼び出すことができます(呼び出されます)。

関連する問題