如何在方法中将函数作为参数传递

时间:2015-02-02 16:44:40

标签: c#

正如标题所说,我该如何实现?到目前为止,这就是我所拥有的。我想在SavePermission方法中传递radsave_click函数。

private void SavePermission(string profileID, string featureID, string ActionID)
{
    var connectionString = ConfigurationManager.ConnectionStrings["Pigen"].ConnectionString;
    using (MySqlConnection cnn = new MySqlConnection(connectionString))
    using (MySqlCommand cmd = cnn.CreateCommand())   
    try
    {
        cnn.Open();
        cmd.CommandText = "sp_perm_insert";
        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.AddWithValue("@pprofileID", Convert.ToInt64(profileID));
        cmd.Parameters.AddWithValue("@pfeatureID", Convert.ToInt64(featureID));
        cmd.Parameters.AddWithValue("@pActionID", Convert.ToInt64(ActionID));
        int result1 = cmd.ExecuteNonQuery();
        cnn.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

我需要将此作为此方法中的参数传递。

private void radbtnSave_Click(object sender, EventArgs e)
{
    try
    {
        int iRow = 0;

        DialogResult result = MessageBox.Show("Do you want to apply these changes?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
        if (result == DialogResult.Yes)
        {
            foreach (DataGridViewRow row in kryptonDataGridViewAction.Rows)
            {
                SavePermission(profileID1, featureID, ActionID);

                string strpro;
                strpro = kryptonDataGridProf.SelectedRows[0].Cells["profileID"].Value.ToString();

                string strfeat;
                strfeat = kryptonDataGridViewAction.Rows[iRow].Cells["featureID"].Value.ToString();

                string strAct;
                strAct = kryptonDataGridViewAction.Rows[iRow].Cells["ActionID"].Value.ToString();

                //string strAct_name;
                //strAct_name = kryptonDataGridViewAction.Rows[iRow].Cells["ACTIONS"].Value.ToString();

                //       MessageBox.Show(strpro, "");
                //       MessageBox.Show(strpro+ " /" + strfeat + " /" + strAct );
                iRow = iRow + 1;
            }
            //  MessageBox.Show("Changes has successfully been applied", "Applying Changes", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

1 个答案:

答案 0 :(得分:1)

您无法更改radbtnSave_Click事件处理程序的签名,但您可以在事件处理程序中使用委托,如下所示:

var a = new Action<string, string, string>(SavePermission);

然后像这样使用它:

a("my profile ID", "my feature ID", "my action ID");

或者您可以直接调用该方法:

SavePermission("my profile ID", "my feature ID", "my action ID");

我想我真的不明白为什么你需要传递这个方法。