我目前正试图教自己如何使用活动,但我似乎无法让一切正常工作。学习活动
与事件编码看起来像这样的类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EventsPractice
{
public delegate void myEventHandler(string newValue);
public class EventExample
{
private string _value;
public event myEventHandler valueChanged;
public string val
{
set
{
_value = value;
valueChanged(_value);
}
}
static void myEvt_valueChanged(string newValue)
{
Console.WriteLine("The value changed to {0}", newValue);
}
}
}
我的计划类看起来是这样的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EventsPractice;
namespace EventsPractice
{
public class Program
{
static void Main(string[] args)
{
EventExample myEvt = new EventExample();
myEvt.valueChanged += myEventHandler(myEvt_valueChanged);
string str;
do
{
str = Console.ReadLine();
if (!str.Equals("exit"))
myEvt.val = str;
} while (!str.Equals("exit"));
}
}
}
我收到的错误是:
“错误1' EventsPractice.myEventHandler'是一个'类型',但像'变量'一样使用C:\ Users \ Administrator \ Documents \ Visual Studio 2010 \ Projects \ EventsPractice \ EventsPractice \ Program.cs 15 35 EventsPractice“
错误2名 'myEvt_valueChanged' 在目前情况下C不存在:\用户\管理\文档\ Visual Studio 2010的\项目\ EventsPractice \ EventsPractice \的Program.cs 15 50 EventsPractice
我不确定我错过了什么或者我错了什么地方。任何帮助,将不胜感激。