c#quiz项目

时间:2011-12-31 06:02:46

标签: c# asp.net

请帮我解决这个错误!这里ResultValue是一个枚举。

描述:编译服务此请求所需的资源时发生错误。请查看以下特定错误详细信息并相应地修改源代码。

编译器错误消息:CS0019:运算符'=='不能应用于'string'和'Answer.ResultValue'类型的操作数

 Source Error:


   Line 38:                 Answer a = (Answer)al[i];
   Line 39: 
   Line 40:                 if (a.Result == Answer.ResultValue.Correct)
   Line 41:                     correct=correct+1;
   Line 42:             }

=================================

 using System;
 using System.Data;
 using System.Configuration;
 using System.Collections;
 using System.Web;
 using System.Web.Security;
 using System.Web.UI;
 using System.Web.UI.WebControls;
 using System.Web.UI.WebControls.WebParts;
 using System.Web.UI.HtmlControls;



  public partial class results : System.Web.UI.Page
 {
  protected void Page_Load(object sender, EventArgs e)
 {
    ArrayList al = (ArrayList)Session["AnswerList"];

    if (al == null)
    {
        Response.Redirect("History.aspx");
    }

    resultGrid.DataSource = al;
    resultGrid.DataBind();

    // Save the results into the database.

    if (IsPostBack == false)
    {
        // Calculate score
        double questions = al.Count;
        double correct = 0.0;


        for (int i = 0; i < al.Count; i++)
        {
            Answer a = (Answer)al[i];

            if (a.Result == Answer.ResultValue.Correct)
                correct=correct+1;
        }

        double score = (correct / questions) * 100;

        SqlDataSource studentQuizDataSource = new SqlDataSource();
        studentQuizDataSource.ConnectionString =             
         ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        studentQuizDataSource.InsertCommand = "INSERT INTO UserQuiz ([QuizID],                 

          [DateTimeComplete], [Score], [UserName]) VALUES (@QuizID, @DateTimeComplete,               
              @Score, @UserName)";

        studentQuizDataSource.InsertParameters.Add("QuizID", 
          Session["QuizID"].ToString());
        studentQuizDataSource.InsertParameters.Add("Score", score.ToString());
        studentQuizDataSource.InsertParameters.Add("UserName", User.Identity.Name);
        studentQuizDataSource.InsertParameters.Add("DateTimeComplete",  
        DateTime.Now.ToString());

        int rowsAffected = studentQuizDataSource.Insert();
        if (rowsAffected == 0)
        {

            errorLabel.Text = "There was a problem saving your quiz results into our 
                               database.  Therefore, the results from this quiz will               
                                    not be displayed on the list on the main menu.";


        }

    }


}

protected void resultGrid_SelectedIndexChanged(object sender, EventArgs e)
{
    SqlDataSource1.FilterExpression = "QuestionOrder=" + resultGrid.SelectedValue;
 }

}

Answer.cs的代码

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.Web.UI;
 using System.Web.UI.WebControls;

 public partial class Answer : System.Web.UI.Page
{
    private string m_QuestionID;
    private string m_CorrectAnswer;
    private string m_UserAns;
    private string m_Result;


    public string QuestionID
    {
        get
        {
            return m_QuestionID;
        }
        set
        {
            m_QuestionID = value;
        }
    }


    public string CorrectAnswer
    {
        get
        {
            return m_CorrectAnswer;
        }
        set
        {
            m_CorrectAnswer = value;
        }
    }


    public string UserAns
    {
        get
        {
            return m_UserAns;
        }
        set
        {
            m_UserAns = value;
        }
    }

    public string Result
    {
        get
        {
            if (m_UserAns == m_CorrectAnswer)
            {
                return "Correct";
            }
            else
            {
                return "Incorrect";
            }
        }
    }

public enum ResultValue
{
    Correct,
    Incorrect
 }


 }

2 个答案:

答案 0 :(得分:2)

很明显,你正在尝试将Enumeration Types与字符串进行比较,这些字符串并不相同,因此无法进行比较。相反,我认为你应该尝试替换这段代码:

public string Result
{
    get
    {
        if (m_UserAns == m_CorrectAnswer)
        {
            return "Correct";
        }
        else
        {
            return "Incorrect";
        }
    }
}

public ResultValue Result
{
    get
    {
        if (m_UserAns == m_CorrectAnswer)
        {
            return ResultValue.Correct;
        }
        else
        {
            return ResultValue.Incorrect;
        }
    }
}

答案 1 :(得分:0)

尝试评估一下: -

if (a.Result == Answer.ResultValue.Correct.ToString())