-2

我可以通过EditQuestionMaster(int qid_value)这个吗qid_value,在这个private void button1_Click(object sender, EventArgs e)按钮内点击,如果是的话,我该怎么做才能UpdateQuestion正确

public partial class EditQuestionMaster : Form
    {
        DbHandling db = new DbHandling();
        public EditQuestionMaster(int qid_value)
        {
            InitializeComponent();
            string subNtop = db.GetEditSubNTopic(qid_value);
            string[] subNtopData = subNtop.Split('~');
            cmbSubject.Text = subNtopData[2];                
        }
private void button1_Click(object sender, EventArgs e)
        {       

            int qid = ; //here i want the value of int qid_value
            string AnsOp = "";
            if (radioButton1.Checked == true)
                AnsOp = "1";
            else if (radioButton2.Checked == true)
                AnsOp = "2";
            else if (radioButton3.Checked == true)
                AnsOp = "3";
            else if (radioButton4.Checked == true)
                AnsOp = "4";
            else
            {
                MessageBox.Show("Answer Option Not Selected");
                return;
            }

            string Marks = cmbMarks.SelectedItem.ToString();

            if (db.UpdateQuestion(qid, txtQuestion.Text, txtOption1.Text, txtOption2.Text, txtOption3.Text, txtOption4.Text, AnsOp, Marks, "T"))
                MessageBox.Show("Question Updated Successfully");
            else
                MessageBox.Show("Failed to Update Question");   
        }
}

提前感谢您的帮助

4

2 回答 2

2
public partial class EditQuestionMaster : Form
    {
        DbHandling db = new DbHandling();
        int qid; // here is the class variable
        public EditQuestionMaster(int qid_value)
        {
            InitializeComponent();

            this.qid = qid_value; // set the value here

            string subNtop = db.GetEditSubNTopic(qid_value);
            string[] subNtopData = subNtop.Split('~');
            cmbSubject.Text = subNtopData[2];                
        }
private void button1_Click(object sender, EventArgs e)
        {       

            qid // use it here
于 2013-08-02T17:50:40.717 回答
0

扩展机会的答案:

您可以定义一个业务对象来处理更新,如下所示:

public class QuestionEditor
{
    DbHandling db = new DbHandling();
    int questionId;
    public QuestionEditor(int questionId)
    {
        this.questionId = questionId;
    }

    public void SetAnswer(string answerOption)
    {
        db.UpdateQuestion(qid, answerOption);
    }
}

创建表单后,让它创建业务对象的新实例:

public partial class EditQuestionMaster : Form
{
    QuestionEditor editor;
    public EditQuestionMaster(int qid_value)
    {
        InitializeComponent();

        editor = new QuestionEditor(qid_value);            
    }
}

现在表单中的其他方法可以调用编辑器来执行操作。

private void button1_Click(object sender, EventArgs e)
{
    string answerOption;

    // switch populates answerOption

    editor.SetAnswer(answerOption);
}

通过将执行更新的逻辑从用户交互的表单中移开,这称为抽象。这种抽象允许 UI 代码在某种程度上独立于问题数据的访问/存储方式而发展。这是 n 层架构的基础。

于 2013-08-02T18:17:04.530 回答