简体   繁体   中英

c# quiz project

Please help me with this error! Here ResultValue is an enumeration.

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0019: Operator '==' cannot be applied to operands of type 'string' and '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;
 }

}

code for 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
 }


 }

It's pretty obvious you're trying to compare Enumeration Types with Strings, which aren't the same and can't be compared like that. Instead, I think you should try replacing this bit of code:

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

with

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

尝试评估:-

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM