在做项目时,业务逻辑和操作分开是必要的.于是就把几个控件作为参数进行传递,是这样实现的:
public void InsertNodes(ArrayList p_ControlList)
{
string strsql="....";
DBCon dbcIns = new DBCon();//数据库连接
SqlCommand cmd = new SqlCommand(strsql, dbcIns.Con);
cmd.Parameters.Add("@Name", SqlDbType.VarChar);
cmd.Parameters.Add("@ParentID", SqlDbType.VarChar);
cmd.Parameters.Add("@TcpPort", SqlDbType.Int);
cmd.Parameters.Add("@TELNum", SqlDbType.VarChar);
//cmd.Parameters["@Name"].Value = p_ControlList[0].ToString();
//cmd.Parameters["@ParentID"].Value = p_ControlList[1].ToString();
TextBox txt_Name = (TextBox)p_ControlList[0];
Label lab_ParentID = (Label)p_ControlList[1];
TextBox txt_TcpPort = (TextBox)p_ControlList[2];
TextBox txt_TelNum = (TextBox)p_ControlList[3];
........
}
是这样调用的:
ManageNode InNodes = new ManageNode();
ArrayList ContolList = new ArrayList();
ContolList.Add(this.txtName);
ContolList.Add(this.labPID);
ContolList.Add(this.txtTCP);
ContolList.Add(this.txtTel);
//NodesInfor MyNodes=new NodesInfor();
InNodes.InsertNodes(ContolList);
OK
相当于把控件传过去时,也把其值传过去了.
这样做优劣各是什么,希望大家踊跃发言呀.
本文介绍了一种在项目中将控件及其值作为参数传递的方法,通过实例展示了如何使用 ArrayList 存储控件并传递给方法,从而实现业务逻辑与操作的分离。

被折叠的 条评论
为什么被折叠?



