|
查看: 1327|回复: 8
|
C#有没有像PHP那样的功能
[复制链接]
|
|
|
在PHP中有这样的功能:
设
$function = "hello";
那么如果 $function(); 就等于执行了 hello()
问C#有类似的功能吗? |
|
|
|
|
|
|
|
|
|
|
发表于 24-6-2006 12:07 AM
|
显示全部楼层
|
call function 不是所有programming language 都有的吗??? |
|
|
|
|
|
|
|
|
|
|
发表于 24-6-2006 01:14 AM
|
显示全部楼层
|
几乎任何一个电脑语言都能呼唤 function... |
|
|
|
|
|
|
|
|
|
|
发表于 24-6-2006 01:27 AM
|
显示全部楼层
他要的應該是function pointer。
C#的delegate就是function pointer。
- void hello() {
- Console.WriteLine("Hello");
- }
- void bye() {
- Console.WriteLine("Bye");
- }
- delegate void fp();
- static int Main() {
- fp function;
- function = new fp(hello);
- function();
- function = new fp(bye);
- function();
- }
复制代码 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 24-6-2006 02:00 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 24-6-2006 02:19 AM
|
显示全部楼层
但是delegate怎么用
我有10个textbox分别bind到同一个dataset中的不同row
我想要在lostFocus的时候update到资料库
不过一个一个去设EventHandler会很累人的……
能用delegate解决吗? |
|
|
|
|
|
|
|
|
|
|
发表于 27-6-2006 09:54 PM
|
显示全部楼层
不需要delegate, 把全部textbox的LostFocus放同一个event handler,再根據sender来決定要保存谁。
- void Form_Load(object sender, System.EventArgs e) {
- textbox0.LostFocus += new EventHandler(textbox_LostFocus);
- textbox1.LostFocus += new EventHandler(textbox_LostFocus);
- textbox2.LostFocus += new EventHandler(textbox_LostFocus);
- }
- void textbox_LostFocus(object sender, System.EventArgs e) {
- Control c = sender as Control;
- DataRow dr = c.DataBinding[0].DataSource as DataRow;
- // 保存
- }
复制代码
DataRow只是一个例子,你必須根據你的DataSource来調整。 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 28-6-2006 10:22 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 27-10-2008 12:49 AM
|
显示全部楼层
回复 8# terrorgen 的帖子
This should be a safer way to deal with the winform UI thread.
private delegate void UpdateTitleDelegate(Control lable,string s);
void Form_Load(object sender, System.EventArgs e) {
textbox0.LostFocus += new EventHandler(textbox_LostFocus);
textbox1.LostFocus += new EventHandler(textbox_LostFocus);
textbox2.LostFocus += new EventHandler(textbox_LostFocus);
}
void textbox_LostFocus(object sender, System.EventArgs e) {
Control c = sender as Control;
UpdateTitle(c, c.Text);
}
void UpdateTitle(Control lable,string newTitle)
{
if (lable.InvokeRequired)
lable.Invoke(new UpdateTitleDelegate(UpdateTitle), newTitle);
else
{
DataRow dr = c.DataBinding[0].DataSource as DataRow;
// 保存
}
} |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|