谁来解释下这段代码
时间:2011-12-26
来源:互联网
protected void Page_Load(object sender, EventArgs e)
{
string value = Request.Form["hName"] ?? "";
bool result;
if (bool.TryParse(value, out result))
{
dic = new Dictionary<bool, Action>();
dic.Add(true, Confirm);
dic.Add(false, Cancel);
dic[result]();
}
}
protected void btnCallBack_Click(object sender, EventArgs e)
{
// 先执行一段代码
lit.Text = DateTime.Now.ToString("yyyy-MM-dd");
//弹出confirm
ClientScript.RegisterStartupScript(GetType(), "confirm", "CustomSubmit();", true);
}
//确认时执行的代码
private void Confirm()
{
lit.Text = "确认";
}
//取消时执行的代码
private void Cancel()
{
lit.Text = "取消";
}
这是非哥博客中对于confirm的改写,Dictionary<bool, Action> dic中ACTION是什么类型,还有我调试的时候if (bool.TryParse(value, out result))中的代码一直没有运行过,这段代码有什么作用
作者: jianghui7897 发布时间: 2011-12-26
作者: jianghui7897 发布时间: 2011-12-26
if (bool.TryParse(value, out result))
这个表示的是 是否可以将value转换为bool类型,如果可以那么走if里面,result是返回值,就是value转换成bool类型后的值。
作者: dd__dd 发布时间: 2011-12-26
Cancel()
从dic = new Dictionary<bool, Action>();
dic.Add(true, Confirm);
dic.Add(false, Cancel);
看出来的
@非哥
作者: jayinIT 发布时间: 2011-12-26
ACTION应该是个枚举类型吧,当然这个只是猜的,具体要看他怎么定义的了。
if (bool.TryParse(value, out result))
这个表示的是 是否可以将value转换为bool类型,如果可以那么走if里面,result是返回值,就是value转换成bool类型后的值。
{
dic = new Dictionary<bool, Action>();
dic.Add(true, Confirm);
dic.Add(false, Cancel);
dic[result]();
} 中if里面的代码一直没执行过,dic一直为null,但是最后直接执行了dic[result]();, dic = new Dictionary<bool, Action>();
dic.Add(true, Confirm);
dic.Add(false, Cancel);一直没执行,但是dic突然就有值了,这里一直很纠结。不知道为什么。
作者: jianghui7897 发布时间: 2011-12-26
鄙人认为action是指 Confirm()
Cancel()
从dic = new Dictionary<bool, Action>();
dic.Add(true, Confirm);
dic.Add(false, Cancel);
看出来的
@非哥
dic = new Dictionary<bool, Action>();
dic.Add(true, Confirm);
dic.Add(false, Cancel);这段代码一直没运行,最后却直接运行了dic[result]();,跳过了dic = new Dictionary<bool, Action>();
dic.Add(true, Confirm);
dic.Add(false, Cancel);这段代码,dic突然出现了值,何解。我把地址发出来吧,希望大家能够帮忙看看http://blog.csdn.net/Sandy945/article/details/5716632
作者: jianghui7897 发布时间: 2011-12-26
作者: dd__dd 发布时间: 2011-12-26
ACTION就是System.Action 委托 具体的你可以查看下MSDN,刚才没仔细看
作者: jianghui7897 发布时间: 2011-12-26
作者: shashengduguzhe 发布时间: 2011-12-26
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28