我自己做的一个委托事件,怎么不好用,望高手解决
控制台程序
CustomerEventArg类代码如下using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication3
{
class CustomerEventArg:EventArgs
{
private string name = "";
private int age = 0;
public string Name
{
get { return this.name; }
set { this.name = value; }
}
public int Age
{
get { return this.age; }
set { this.age = value; }
}
}
}Admin类代码如下using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication3
{
class Admin
{
public void Notify(Object sender,CustomerEventArg e)
{
System.Console.WriteLine(e.Name + "is" + e.Age.ToString());
}
}
}
Employee类的代码如下using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication3
{
public delegate void DelegateClassHandle(Object sender,CustomerEventArg e);
class Employees
{
private string _name;
public string Name
{
get{return this._name;}
set{this._name=value;}
}
private int _age;
public int Age
{
get{return this._age;}
set{this._age=value;}
}
public event DelegateClassHandle PlayGame;
public void Games()
{
if(PlayGame!=null){
CustomerEventArg e=new CustomerEventArg();
e.Name=this._name;
e.Age=this._age;
PlayGame(this,e);
}
}
}
}test类的代码如下using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication3
{ class test
{
static void Main(string[] args)
{
Employees employee = new Employees();
employee.Name = "Mike";
employee.Age = 25;
Admin admin = new Admin();
employee.PlayGame += new DelegateClassHandle(admin.Notify);
employee.Games();
}
}
}运行之后有错误是
------ Build started: Project: ConsoleApplication3, Configuration: Debug x86 ------
c:\users\administrator\documents\visual studio 2010\Projects\ConsoleApplication3\ConsoleApplication3\Employee.cs(8,26): error CS0059: Inconsistent accessibility: parameter type 'ConsoleApplication3.CustomerEventArg' is less accessible than delegate 'ConsoleApplication3.DelegateClassHandle'
c:\users\administrator\documents\visual studio 2010\Projects\ConsoleApplication3\ConsoleApplication3\CustomerEventArg.cs(9,11): (Related location)Compile complete -- 1 errors, 0 warnings
Build started 2010/7/15 10:24:18.
ResolveAssemblyReferences:
A TargetFramework profile exclusion list will be generated.
GenerateTargetFrameworkMonikerAttribute:
Skipping target "GenerateTargetFrameworkMonikerAttribute" because all output files are up-to-date with respect to the input files.
CoreCompile:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Csc.exe /noconfig /nowarn:1701,1702 /nostdlib+ /platform:x86 /errorreport:prompt /warn:4 /define:DEBUG;TRACE /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\Microsoft.CSharp.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\mscorlib.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Core.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Data.DataSetExtensions.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Data.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Xml.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Xml.Linq.dll" /debug+ /debug:full /filealign:512 /optimize- /out:obj\x86\Debug\ConsoleApplication3.exe /target:exe Admin.cs CustomerEventArg.cs Employee.cs test.cs Properties\AssemblyInfo.cs "C:\Users\Administrator\AppData\Local\Temp\.NETFramework,Version=v4.0,Profile=Client.AssemblyAttributes.cs"Build FAILED.Time Elapsed 00:00:00.17
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========不知道是那个地方错了,希望高手解决
控制台程序
CustomerEventArg类代码如下using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication3
{
class CustomerEventArg:EventArgs
{
private string name = "";
private int age = 0;
public string Name
{
get { return this.name; }
set { this.name = value; }
}
public int Age
{
get { return this.age; }
set { this.age = value; }
}
}
}Admin类代码如下using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication3
{
class Admin
{
public void Notify(Object sender,CustomerEventArg e)
{
System.Console.WriteLine(e.Name + "is" + e.Age.ToString());
}
}
}
Employee类的代码如下using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication3
{
public delegate void DelegateClassHandle(Object sender,CustomerEventArg e);
class Employees
{
private string _name;
public string Name
{
get{return this._name;}
set{this._name=value;}
}
private int _age;
public int Age
{
get{return this._age;}
set{this._age=value;}
}
public event DelegateClassHandle PlayGame;
public void Games()
{
if(PlayGame!=null){
CustomerEventArg e=new CustomerEventArg();
e.Name=this._name;
e.Age=this._age;
PlayGame(this,e);
}
}
}
}test类的代码如下using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication3
{ class test
{
static void Main(string[] args)
{
Employees employee = new Employees();
employee.Name = "Mike";
employee.Age = 25;
Admin admin = new Admin();
employee.PlayGame += new DelegateClassHandle(admin.Notify);
employee.Games();
}
}
}运行之后有错误是
------ Build started: Project: ConsoleApplication3, Configuration: Debug x86 ------
c:\users\administrator\documents\visual studio 2010\Projects\ConsoleApplication3\ConsoleApplication3\Employee.cs(8,26): error CS0059: Inconsistent accessibility: parameter type 'ConsoleApplication3.CustomerEventArg' is less accessible than delegate 'ConsoleApplication3.DelegateClassHandle'
c:\users\administrator\documents\visual studio 2010\Projects\ConsoleApplication3\ConsoleApplication3\CustomerEventArg.cs(9,11): (Related location)Compile complete -- 1 errors, 0 warnings
Build started 2010/7/15 10:24:18.
ResolveAssemblyReferences:
A TargetFramework profile exclusion list will be generated.
GenerateTargetFrameworkMonikerAttribute:
Skipping target "GenerateTargetFrameworkMonikerAttribute" because all output files are up-to-date with respect to the input files.
CoreCompile:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Csc.exe /noconfig /nowarn:1701,1702 /nostdlib+ /platform:x86 /errorreport:prompt /warn:4 /define:DEBUG;TRACE /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\Microsoft.CSharp.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\mscorlib.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Core.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Data.DataSetExtensions.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Data.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Xml.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Xml.Linq.dll" /debug+ /debug:full /filealign:512 /optimize- /out:obj\x86\Debug\ConsoleApplication3.exe /target:exe Admin.cs CustomerEventArg.cs Employee.cs test.cs Properties\AssemblyInfo.cs "C:\Users\Administrator\AppData\Local\Temp\.NETFramework,Version=v4.0,Profile=Client.AssemblyAttributes.cs"Build FAILED.Time Elapsed 00:00:00.17
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========不知道是那个地方错了,希望高手解决
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货