[英]c# quiz project
Please help me with this error! 请帮我解决这个错误! Here ResultValue is an enumeration. 这里ResultValue是一个枚举。
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' 编译器错误消息: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;
}
code for Answer.cs 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())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.