这么简单的程序都调不通,请求高手帮助!

rzg 2002-01-24 12:03:14
代码如下:
using System;

delegate int MyDelegate();

public class MyClass
{
public void InstanceMethod(){
Console.WriteLine("Call the Static mecthod");
//return 0;
}
static pulic viod StaticMethod() {
Console.WriteLine("Call the static method");
//return 0;
}
}

public class Test
{

static public void Main()
{
MyClass p=new MyClass();
MyDelegate d= new MyDelegate(p.InstanceMethod);
d();
d=new MyDelegate(MyClass.StaticMethod);
d();
}
}

错误为:Test01.cs(11,21): error CS1002: ; expected
请问这是为何?
...全文
139 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
rzg 2002-01-25
  • 打赏
  • 举报
回复
losenetway() 谢谢你啊,我手头上没有vs.net刚刚入门。不知道如何下载!
rzg 2002-01-24
  • 打赏
  • 举报
回复
错误为:Test01.cs(11,21): error CS1002: ; expected
龙腾九霄 2002-01-24
  • 打赏
  • 举报
回复
把出错信息报过来
rzg 2002-01-24
  • 打赏
  • 举报
回复
不行啊!还是有错误。我想用类似函数指针(更确切的说是引用)来调用此方法。
rzg 2002-01-24
  • 打赏
  • 举报
回复
是C#中使用的一个引用类型-代表(delegate),他实际上相当于C#中函数
的指针原型。
龙腾九霄 2002-01-24
  • 打赏
  • 举报
回复
应为:
static public void StaticMethod() {
Console.WriteLine("Call the static method");
//return 0;
静态函数不应该用实例调用,直接用类型名来限定!如下。
MyClass.StaticMethod();
问一下你这里的Delegate到底派什么用场?

losenetway 2002-01-24
  • 打赏
  • 举报
回复
using System;
delegate int MyDelegate();
public class MyClass{
public int InstanceMethod(){
Console.WriteLine("Call the Static mecthod");
return 0;
}
public static int StaticMethod(){
Console.WriteLine("Call the static method");
return 0;
}
}
public class Test{
static public void Main(){
MyClass p=new MyClass();
MyDelegate d= new MyDelegate(p.InstanceMethod);
d();
d=new MyDelegate(MyClass.StaticMethod);
d();
}
}


//为什么不用vs.net进行开发呢?用个UltraEdit也行啊
龙腾九霄 2002-01-24
  • 打赏
  • 举报
回复
Delegate int MyDelegate()改成void MyDelegate()
因为需要和MyClass中的Method匹配,以上程序已经测试通过。。
inside1 2002-01-24
  • 打赏
  • 举报
回复
首先 static pulic viod StaticMethod() {的void拼错了!
其次 MyDelegate是返回值是int的,如何让它产生指向返回值是void的函数的实例
龙腾九霄 2002-01-24
  • 打赏
  • 举报
回复
using System;
delegate void MyDelegate();

namespace MyApp1
{
/// <summary>
/// Summary description for MyClass.
/// </summary>


public class MyClass
{
public void InstanceMethod()
{
Console.WriteLine("Call the Static1 mecthod");
//return 0;
}
static public void StaticMethod()
{
Console.WriteLine("Call the static2 method");
//return 0;
}
}

public class Test
{

static public void Main()
{
MyClass p=new MyClass();
MyDelegate d= new MyDelegate(p.InstanceMethod);
d();
d=new MyDelegate(MyClass.StaticMethod);
d();
//Delegate d=Delegate.CreateDelegate(System.Type.Missing,p.InstanceMethod)


//d();
//d=new MyDelegate();
//d();
}
}

}
RockyZhang 2002-01-24
  • 打赏
  • 举报
回复
还有11行的void写错了。
而且你的MyDelegate定义的是int
执行MyDelegate d= new MyDelegate(p.InstanceMethod);一定会不匹配的
RockyZhang 2002-01-24
  • 打赏
  • 举报
回复
11行写错了一个词,public少了个b
龙腾九霄 2002-01-24
  • 打赏
  • 举报
回复
let me try again..

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧