初始化时使用委托,并用回调函数实现,但是编译不通过,请帮忙解决啊
代码如下:public class CTest
{
#region 委托定义
public delegate void InitCTestDelegate(ref double ditem, ref int iitem, ref string sitem);
public static InitCTestDelegate InitAgent = null;
#endregion
#region 局部变量
private double _ditem = 0;
private int _iitem = 0;
private string _sitem = "";
#endregion
public CTest()
{
if (null != InitAgent)
{
InitAgent(ref _ditem, ref _iitem, ref _sitem);
}
}
}
class Program
{
double _ditem = 2.2;
int _iitem = 2;
string _sitem = "你好!";
static void Main(string[] args)
{
CTest.InitAgent += new CTest.InitCTestDelegate(OnInitVail);
}
private void OnInitVail(ref double ditem, ref int iitem, ref string sitem)
{
ditem = _ditem;
iitem = _iitem;
sitem = _sitem;
}
}
编译报错:错误 1 非静态的字段、方法或属性“ConsoleApplication1.Program.OnInitVail(ref double, ref int, ref string)”要求对象引用 E:\委托和回调\ConsoleApplication1\ConsoleApplication1\Program.cs 17 32 ConsoleApplication1
--------------------编程问答-------------------- 静态成员不能引用非静态成员 --------------------编程问答-------------------- CTest c=newCTest();
c.InitAgent += new CTest.InitCTestDelegate(OnInitVail);
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)
http://feiyun0112.cnblogs.com/ --------------------编程问答-------------------- CTest c=newCTest();
c.InitAgent += new CTest.InitCTestDelegate(OnInitVail);
--------------------------------------------------------------
错误:
怎么可以用实例化的对象去调用静态方法呢? --------------------编程问答-------------------- CTest.InitAgent += new CTest.InitCTestDelegate(OnInitVail);
你自己这里,也是静态成员引用非静态成员。 --------------------编程问答-------------------- public class CTest
{
#region 委托定义
public delegate void InitCTestDelegate(ref double ditem, ref int iitem, ref string sitem);
public static InitCTestDelegate InitAgent = null;
#endregion
#region 局部变量
private double _ditem = 0;
private int _iitem = 0;
private string _sitem = "";
#endregion
public CTest()
{
if (null != InitAgent)
{
InitAgent(ref _ditem, ref _iitem, ref _sitem);
}
}
}
class Program
{
double _ditem = 2.2;
int _iitem = 2;
string _sitem = "你好!";
static void Main(string[] args)
{
Program a = new Program();
a.abc();
}
private void abc()
{
CTest.InitAgent += new CTest.InitCTestDelegate(OnInitVail);
}
private void OnInitVail(ref double ditem, ref int iitem, ref string sitem)
{
ditem = _ditem;
iitem = _iitem;
sitem = _sitem;
}
} --------------------编程问答-------------------- 我自己搞定了
(*^__^*) 嘻嘻……
代码如下:
public class CTest
{
#region 委托定义
public delegate void InitCTestDelegate(ref double ditem, ref int iitem, ref string sitem);
public static InitCTestDelegate InitAgent = null;
#endregion
#region 局部变量
private double _ditem = 0;
private int _iitem = 0;
private string _sitem = "";
#endregion
public CTest()
{
if (null != InitAgent)
{
InitAgent(ref _ditem, ref _iitem, ref _sitem);
}
}
}
class Program
{
double _ditem = 2.2;
int _iitem = 2;
string _sitem = "你好!";
static void Main(string[] args)
{
CTest.InitAgent += new CTest.InitCTestDelegate(OnInitVail);
}
private void OnInitVail(ref double _ditem, ref int _iitem, ref string _sitem)
{
_ditem=2.3;
// iitem = _iitem;
_sitem="你好啊!";
}
}
--------------------编程问答-------------------- 遮阳类CTest的成员_sitem的值就改变了 --------------------编程问答-------------------- 顶
--------------------编程问答-------------------- ?你的改对啦,我怎么用你的代码编译有问题啊?
那个OnInitVail()函数应该是static的吧,那样才是对的啊!
你的委托对象是静态的,那个函数就应该是镜头的啊! --------------------编程问答-------------------- 我重新建立一个窗口工程,不是之前的控制台程序,因为控制台程序在static void Main()中调用我写的委托,貌似不行.
重新建立一个窗口工程
定义下面的一个类:
public class ClassA
{
private string _item1 = "";
private string _item2 = "";
private string _item3 = "";
public delegate void InitCTestDelegate(ref string item1, ref string item2, ref string item3);
public static InitCTestDelegate InitAgent = null;
public ClassA()
{
if (null != InitAgent)
{
InitAgent(ref _item1, ref _item2, ref _item3);
}
}
public string Item1
{
get
{
return _item1;
}
set
{
_item1 = value;
}
}
public string Item2
{
get
{
return _item2;
}
set
{
_item2 = value;
}
}
}
然后在窗口的load事件里写上,load函数不是静态的
private void Form1_Load(object sender, EventArgs e)
{
string item1 = "";
string item2 = "";
string item3 = "";
ClassA.InitAgent = OnInitial;
ClassA a = new ClassA();
MessageBox.Show(a.Item1.ToString()+a.Item2.ToString());
}
private void OnInitial(ref string item1, ref string item2, ref string item3)
{
item1 = "kurney";
item2 = "你好";
item3 = "卧室";
} --------------------编程问答-------------------- 如果使用控制台程序那么原来的程序要改为
double _ditem = 2.2;
int _iitem = 2;
string _sitem = "你好!";
static void Main(string[] args)
{
CTest.InitAgent += new CTest.InitCTestDelegate(OnInitVail);
}
private static void OnInitVail(ref double _ditem, ref int _iitem, ref string _sitem)
{
_ditem=2.3;
// iitem = _iitem;
_sitem="你好啊!";
}
--------------------编程问答-------------------- 那恭喜
补充:.NET技术 , C#