c#利用委托传递函数参数(1)

本次旨在解决 同参不同名 的函数作为参数传递的情况

 

情景:

一下两个函数分别多次重复调用了两个同参不同名的函数(实际上总共有3个这样的函数),函数结构基本相同,只有调用的函数名不一样,显然可以整合在一个函数中,减少代码量;

 1 private void button1_Click(object sender, EventArgs e)
 2         {
 3             clsLineSeg.UpdateDiseasePostion(map, tblRouteData, "Z001522600");
 4             clsLineSeg.UpdateDiseasePostion(map, tblRouteData, "C800522635");
 5             clsLineSeg.UpdateDiseasePostion(map, tblRouteData, "X890522635");
 6             clsLineSeg.UpdateDiseasePostion(map, tblRouteData, "Y003522635");
 7             clsLineSeg.UpdateDiseasePostion(map, tblRouteData, "X899522635");
 8             clsLineSeg.UpdateDiseasePostion(map, tblRouteData, "C018522635");
 9             clsLineSeg.UpdateDiseasePostion(map, tblRouteData, "S311520000");
10             MessageBox.Show("ok");
11         }        
12 
13         private void btnUpdateAssetGps_Click(object sender, EventArgs e)
14         {
15             clsLineSeg.UpdateAssetDataGpsPostion(map, tblRouteData, "Z001522600");
16             clsLineSeg.UpdateAssetDataGpsPostion(map, tblRouteData, "C800522635");
17             clsLineSeg.UpdateAssetDataGpsPostion(map, tblRouteData, "X890522635");
18             clsLineSeg.UpdateAssetDataGpsPostion(map, tblRouteData, "Y003522635");
19             clsLineSeg.UpdateAssetDataGpsPostion(map, tblRouteData, "X899522635");
20             clsLineSeg.UpdateAssetDataGpsPostion(map, tblRouteData, "C018522635");
21             clsLineSeg.UpdateAssetDataGpsPostion(map, tblRouteData, "S311520000");
22             MessageBox.Show("ok");
23         }

 

方案:

但是要想完全统一,肯定需要将要调用的函数作为新函数的参数传递进去,这里就可以用到c#的委托,如下:

    public delegate void UpdateMethodHandler(IMap map, ITable tblRouteData,string routeCode);//声明委托
 1 //修改简化待调用函数
 2 public void UpdateAssetDataGps84Postion(IMap map, ITable tblRouteData, string routeCode)
 3         {
 4             clsLineSeg.UpdateAssetDataGps84Postion(map, tblRouteData, routeCode);
 5         }
 6 
 7         public void UpdateDiseasePostion(IMap map, ITable tblRouteData, string routeCode)
 8         {
 9             clsLineSeg.UpdateDiseasePostion(map, tblRouteData, routeCode);
10         }
 1 //利用委托传递函数名参数
 2         public void btnUpdateClick(UpdateMethodHandler UpdateMethodHandler)
 3         {
 4             var routeLst = GetRouteLst();
 5             if(routeLst==null||routeLst.Length<=0)
 6             {
 7                 MessageBox.Show("请输入路线编码!");
 8                 return;
 9             }
10             foreach(var r in routeLst)
11             {
12                 UpdateMethodHandler(map, tblRouteData, r);
13             }
14             MessageBox.Show("ok");
15         }
 1 //函数调用简化
 2         private void btnUpdateStakeTable_Click(object sender, EventArgs e)
 3         {
 4             btnUpdateClick(UpdateAssetDataGps84Postion);            
 5             //clsLineSeg.UpdateAssetDataGps84Postion(map, tblRouteData, "Z001522600");
 6             //clsLineSeg.UpdateAssetDataGps84Postion(map, tblRouteData, "C800522635");
 7             //clsLineSeg.UpdateAssetDataGps84Postion(map, tblRouteData, "X890522635");
 8             //clsLineSeg.UpdateAssetDataGps84Postion(map, tblRouteData, "Y003522635");
 9             //clsLineSeg.UpdateAssetDataGps84Postion(map, tblRouteData, "X899522635");
10             //clsLineSeg.UpdateAssetDataGps84Postion(map, tblRouteData, "C018522635");
11             //clsLineSeg.UpdateAssetDataGps84Postion(map, tblRouteData, "S311520000");
12             //MessageBox.Show("ok");
13         }      

 

posted @ 2017-02-09 11:48  风谷逍遥客  阅读(1476)  评论(0编辑  收藏  举报