A single method Accepting 3 arguments or 4 arguments
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.
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!)
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 is your friend.
eg:
public static void fillCheckList(string ListType,int RecordNum,CheckBox chkRequired, params TextBox[] txtBoxes)
{
TextBox txtComplete = null;
TextBox txtMemo = null;
TextBox txtThirdOne = null;
if(txtBoxes.Length < 1)
{
throw new Exception("At least the txtComplete-Textbox has to be given");
}
else
{
txtComplete = txtBoxes[0];
if(txtBoxes.Length >= 2)
txtMemo = txtBoxes[1];
if(txtBoxes.Length >= 3)
txtThirdOne = txtBoxes[2];
}
// do stuff
}
Check the keyword params
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.
public static void fillCheckList(string ListType, int RecordNum, CheckBox chkRequired, TextBox txtComplete)
{
fillCheckList(ListType, RecordNum, chkRequired, txtComplete, null);
}
You have 3 options
1.
public static void fillCheckList(string ListType, int RecordNum, CheckBox chkRequired, TextBox txtComplete, TextBox txtMemo) {
fillCheckList(ListType, RecordNum, chkRequired, txtComplete, txtMemo, null);
}
public static void fillCheckList(string ListType, int RecordNum, CheckBox chkRequired, TextBox txtComplete,TextBox txtMemo,TextBox txtMemo, TextBox txtMemo) {
// implementation
}
public static void fillCheckList(string ListType, int RecordNum, CheckBox chkRequired, IEnumerable textBoxes) { // implementation }
3.
public static void fillCheckList(string ListType, int RecordNum, CheckBox chkRequired, params TextBox[] textBoxes) {
// implementation
}
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.
精彩评论