单个方法接受 3 个参数或 4 个参数

发布于 2024-08-23 13:38:34 字数 1897 浏览 5 评论 0原文

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

硪扪都還晓 2024-08-30 13:38:34

不要为此使用 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!)

凉月流沐 2024-08-30 13:38:34

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.

对你再特殊 2024-08-30 13:38:34

params 是你的朋友。

例如:

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    
}

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    
}
海未深 2024-08-30 13:38:34

检查关键字 params

Check the keyword params

相思碎 2024-08-30 13:38:34

您可以使用 params 但它要求您具有相同类型的随机数量的参数或具有松散类型的参数集合(对象)

您还可以使用 5 个参数编写函数,并提供一个具有 4 个参数的重载方法,该方法调用前者并默认最后一个参数。

public static void fillCheckList(string ListType, int RecordNum, CheckBox chkRequired, TextBox txtComplete)
        {
            fillCheckList(ListType, RecordNum, chkRequired, txtComplete, null);
        }

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);
        }
旧故 2024-08-30 13:38:34

您有 3 个选项

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
}
  1. public static void fillCheckList(string ListType, int RecordNum, CheckBox chkRequired, IEnumerable textBoxes) {
    // 执行
    }

3.

public static void fillCheckList(string ListType, int RecordNum, CheckBox chkRequired, params TextBox[] textBoxes) {
         // implementation
    }

如果您需要添加不同的成员并且您的可能性集有限,则选项 1 是可以的。

选项2. 可以。但不像 3 那样优雅。

选项 3 可能是最好的选择,只要添加的参数有一个共同的祖先 - 对象(如果必须的话)。请记住,params 始终必须是最后一个参数。

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
}
  1. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文