在main中调用事件时传递参数

时间:2013-11-16 17:34:18

标签: c#

我有OnGoal活动,我有两个来自自定义类团队的团队如何将团队类型对象传递给事件以更改分数值我很困惑这里是我的代码:

public delegate void  TeamDeligate(Team _team);
public  void Game_Start(object starttime,Team _away,Team _home)
{
    string text;
    var game = new Game();
    DateTime StartTime = (DateTime)starttime;
    Console.WriteLine("Game Starts!");
    Console.Write("Q1:blablabla?");
    Show_Left(StartTime);
    text=Console.ReadLine();
    if (text == "goal away") game.OnGoal += game_OnGoal(_away);

}

void game_OnGoal(Team _team)
{
    _team.Score++;
}

这是我的团队课程:

public class Game
{
    public Team Home { get; set; }
    public Team Away { get; set; }
    public Team Win { get; set; }

    public event TeamDeligate OnGoal;

}

1 个答案:

答案 0 :(得分:1)

基本上,您使用的方式不正确。在书"C# in a Nutshell"中,有一个很好的标准事件模式实现示例: http://my.safaribooksonline.com/book/programming/csharp/9781449334192/events/id4257031

传递参数到事件处理程序,事件源,事件args它就在那里。

相关问题