单个方法接受 3 个参数或 4 个参数
public static void fillCheckList(string ListType,int RecordNum,CheckBox chkRequired,TextBox txtComplete,TextBox txtMemo)
{
string sql_Check = String.Format(@"SELECT l.Required,l.Completed,l.ISP,l.Memo_Notes,
t.List_Desc
FROM List_Data l, List_Type t
WHERE l.List_ID = t.List_ID
AND l.Record_Num = {0}
AND t.List_Desc = '{1}'", RecordNum,ListType);
ListData LIST = new ListData();
SqlConnection sqlConn = null;
SqlCommand cmd_Check;
SqlDataReader dr_Check;
try
{
sqlConn = new SqlConnection(databaseConnectionString);
sqlConn.Open();
cmd_Check = new SqlCommand(sql_Check, sqlConn);
dr_Check = cmd_Check.ExecuteReader();
while (dr_Check.Read())
{
LIST = new ListData(Convert.ToBoolean(dr_Check["Required"]), dr_Check["Completed"].IsNull() ? (DateTime?)null : Convert.ToDateTime(dr_Check["Completed"]), dr_Check["Memo_Notes"].ToString());
}
chkRequired.Checked = LIST.REQUIRED;
txtComplete.Text = LIST.COMPLETED.HasValue ? LIST.COMPLETED.Value.ToShortDateString() : "";
txtMemo.Text = LIST.MEMO_NOTES;
}
catch (Exception e)
{
MessageBox.Show("Error found in fillCheckList..." + Environment.NewLine + e.ToString());
}
finally
{
if (sqlConn != null)
{
sqlConn.Close();
}
}
}
正如您所看到的,我采用一个整数变量作为项目 ID,一个字符串变量作为列表类型,以及 2 个文本框类型。 因此我使用这个方法来接受 4 个参数..我想做的是我也希望它接受 5 个参数。也就是说..再包含一个文本框.. 这样它要么相应地需要 4 个参数或 5 个参数/ 我该如何用同样的方法做到这一点。
public static void fillCheckList(string ListType,int RecordNum,CheckBox chkRequired,TextBox txtComplete,TextBox txtMemo)
{
string sql_Check = String.Format(@"SELECT l.Required,l.Completed,l.ISP,l.Memo_Notes,
t.List_Desc
FROM List_Data l, List_Type t
WHERE l.List_ID = t.List_ID
AND l.Record_Num = {0}
AND t.List_Desc = '{1}'", RecordNum,ListType);
ListData LIST = new ListData();
SqlConnection sqlConn = null;
SqlCommand cmd_Check;
SqlDataReader dr_Check;
try
{
sqlConn = new SqlConnection(databaseConnectionString);
sqlConn.Open();
cmd_Check = new SqlCommand(sql_Check, sqlConn);
dr_Check = cmd_Check.ExecuteReader();
while (dr_Check.Read())
{
LIST = new ListData(Convert.ToBoolean(dr_Check["Required"]), dr_Check["Completed"].IsNull() ? (DateTime?)null : Convert.ToDateTime(dr_Check["Completed"]), dr_Check["Memo_Notes"].ToString());
}
chkRequired.Checked = LIST.REQUIRED;
txtComplete.Text = LIST.COMPLETED.HasValue ? LIST.COMPLETED.Value.ToShortDateString() : "";
txtMemo.Text = LIST.MEMO_NOTES;
}
catch (Exception e)
{
MessageBox.Show("Error found in fillCheckList..." + Environment.NewLine + e.ToString());
}
finally
{
if (sqlConn != null)
{
sqlConn.Close();
}
}
}
As you can see, i take in an integer variable for the project id, a string variable for list type, and 2 text box type.
i am thus using this method to accept 4 arguments.. what i want to do is that i also want it to accept 5 arguments . that is.. include one more text box..
so that it either takes 4 arguments or 5 arguments accordingly/
how do i do that in the same method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不要为此使用 params。
使用 params 来表示这样的想法:“我可以采用零个、一个或任意多个额外参数。听起来你想采用零或一个额外的参数
是正确的;您应该使用一个可选参数(在 C# 4 中),或者编写两个方法并让其中一个方法使用额外参数的默认值调用另一个方法
(最好是 后者。修复 SQL 注入漏洞!)
Do not use params for this.
Use params to represent the idea "I can take zero, one, or arbitrarily many extra parameters. It sounds like you want to take zero or one extra parameters.
TJMonk15 is right; you should either use an optional parameter (in C# 4), or write two methods and have one of them call the other with a default value for the extra parameter. Preferably the latter.
(And for goodness sake fix that SQL injection vuln!)
如果您使用的是 c# 4.0,则可以使用 可选参数。如果没有,您应该使用方法重载,并使用一个重载来调用另一个重载,并为最后一个参数设置默认值。
If you are using c# 4.0 you can use Optional Arguments. If not, you should use method overloading and using one overload to call the other with a default value for the last parameter.
params 是你的朋友。
例如:
params is your friend.
eg:
检查关键字 params
Check the keyword params
您可以使用 params 但它要求您具有相同类型的随机数量的参数或具有松散类型的参数集合(对象)
您还可以使用 5 个参数编写函数,并提供一个具有 4 个参数的重载方法,该方法调用前者并默认最后一个参数。
You can use params but it requires either you to have a random number of arguments of the same type or having a loose typed collection of arguments (object)
You can also write your function with 5 arguments and provide an overloaded method with 4 arguments that calls the former and defaults the last parameter.
您有 3 个选项
1.
public static void fillCheckList(string ListType, int RecordNum, CheckBox chkRequired, IEnumerable textBoxes) {
// 执行
}
3.
如果您需要添加不同的成员并且您的可能性集有限,则选项 1 是可以的。
选项2. 可以。但不像 3 那样优雅。
选项 3 可能是最好的选择,只要添加的参数有一个共同的祖先 - 对象(如果必须的话)。请记住,params 始终必须是最后一个参数。
You have 3 options
1.
public static void fillCheckList(string ListType, int RecordNum, CheckBox chkRequired, IEnumerable textBoxes) {
// implementation
}
3.
Option 1. is ok if you need to add different members and you have a finite set of possibilities.
Option 2. is ok. But not a elegant as 3.
Option 3. is maybe the best thing to do, as long as the added parameters have a common ancestor - object if it must be. Remember that params alway has to be the last parameter.