2016-04-11 8 views
0

私はイベントを作るトラブルのビットを持っていtyoeデリゲートでなければなりませんが、私はエラー'IStregsystem.UserBalanceWarning': event must be of delegate type、 を取得していますが、私は正確に間違っているかを把握することはできません宣言イベント - 。「イベントが

。私のインターフェイス、IStregsystemは私が実装する必要がラインevent UserBalanceNotification UserBalanceWarning;を、持っている私のイベント・クラスは、この(私はちょうど今のところテストしてる)のように見えます

:。

using System; 

namespace Eksamensopgave2016 
{ 
    public class UserBalanceNotification 
    { 
     public delegate void UserBalanceMessage(User user, decimal balanceDifference); 
     public event UserBalanceMessage UserBalanceWarning; 
     public User user { get; set; } 
     private decimal _balanceDifference { get; } 
     public void OnUserBalanceWarning() 
     { 
      if (UserBalanceWarning != null) 
      { 
       UserBalanceWarning(user, _balanceDifference); 
      } 
      else 
      { 
       Console.WriteLine("Event fired!"); 
      } 
     } 
     public UserBalanceNotification(User user, decimal balanceDifference) 
     { 
      _balanceDifference = balanceDifference; 
      SetValue(user, balanceDifference); 
     } 

     public void SetValue(User user, decimal balanceDifference) 
     { 
      if (balanceDifference != 0) 
      { 
       user.Balance += balanceDifference; 
       if (user.Balance < 50) 
       { 
        OnUserBalanceWarning(); 
       } 
      }   
     } 
    } 
} 

私は約ちょうど少し不確かですイベント。 行

​​

UserBalanceWarningですか?それともプロパティですか?

ご協力いただきありがとうございます。

マイIStregsystem:IStregsystemインタフェースで

using System; 
using System.Collections.Generic; 
using Eksamensopgave2016.Core; 

namespace Eksamensopgave2016 
{ 
    public interface IStregsystem 
    { 
     IEnumerable<Product> ActiveProducts { get; } 
     List<User> Users { get; set; } 
     Queue<Transaction> Transactions { get; set; } 
     InsertCashTransaction AddCreditsToAccount(User user, int amount); 
     BuyTransaction BuyProduct(User user, Product product); 
     Product GetProductByID(int productID); 
     IEnumerable<Transaction> GetTransactions(User user, int count); 
     List<User> GetUsers(IFilter<User> filter); 
     User GetUserByUsername(string username); 
     event UserBalanceNotification UserBalanceWarning; 
    } 
} 
+1

*は、メソッドをUserBalanceWarningであるか、それは財産である*それはイベントです?。 –

+0

IStregsystemとUserBalanceNotificationのコードを表示できますか? – Nikolay

+0

@Nikolay私はすでにOPに自分のUserBalanceNotification.csファイル全体を持っています。 IStregsystemを含めるように投稿を編集しました。 – jmkjaer

答えて

1

、あなたはイベントハンドラとしてクラスタイプ(UserBalanceNotification)を使用しています。これにより、前述のコンパイル時エラーが発生します。

イベントのハンドラはデリゲート型である必要があります。 UserBalanceNotificationクラスで正しい方法を調べることができます。次のコードでイベントに

public class ClassWithEvent 
{ 
    public delegate void CustomEventHandler(User user, decimal balanceDifference); 
    public event CustomEventHandler CustomEvent; 

    private void FireCustomEvent(User user, decimal balanceDifference) 
    { 
     if (CustomEvent != null) 
     { 
      CustomEvent(user, balanceDifference); 
     } 
    } 
} 

登録:

public class ClassUsingEvent 
{ 
    private ClassWithEvent _classWithEvent; 

    public ClassUsingEvent() 
    { 
     _classWithEvent = new ClassWithEvent(); 
     _classWithEvent.CustomEvent += HandleCustomEvent; 
    } 

    void HandleCustomEvent(User user, decimal balanceDifference) 
    { 
     // handle event 
    } 
} 
+0

ありがとう!私はもう少し見る必要がありますが、本当にあなたの答えをありがとう! – jmkjaer

関連する問題