如何解决我如何在一个函数中使用列表,而在另一个函数中添加了一些数据
这是我的第一个文件 QuizzyService:
List<Question> newQuestions = new List<Question>();
public static List<Question> QuizzyServiceQuestions()
{
using (Stream stream = File.Open("..\\Debug\\questions.bin",FileMode.Open))
{
var binaryFormatter = new BinaryFormatter();
return (List<Question>)binaryFormatter.Deserialize(stream);
}
}
int curQuestion = 0;
public Question NewGame(int questionCount)
{
Random r = new Random();
for(int i = 0; i < questionCount; i++)
{
Question x = QuizzyServiceQuestions()[r.Next(0,QuizzyServiceQuestions().Count)];
while (newQuestions.Contains(x))
{
x = QuizzyServiceQuestions()[r.Next(0,QuizzyServiceQuestions().Count)];
}
newQuestions.Add(x);
}
Console.WriteLine(newQuestions.Count);
return newQuestions[0];
}
public int CheckAnswer(int questionId,int answerId)
{
List<int> IdList = new List<int>();
for (int i = 0; i < QuizzyServiceQuestions().Count; i++)
{
IdList.Add(QuizzyServiceQuestions()[i].Id);
}
return IdList.Single(i => i == questionId);
}
public Question GetNextQuestion()
{
curQuestion += 1;
Console.WriteLine(newQuestions.Count);
//Console.WriteLine(curQuestion);
//Console.WriteLine(this.newQuestions.Count);
return newQuestions[curQuestion];
}
这里是 xaml.cs 文件的相关部分:
private int totalQuestionCount;
private int currentQuestionCount = 1;
private int correctAnswerCount;
Question newQuestion = new Question();
public MainWindow()
{
InitializeComponent();
}
private void btnNewGame_Click(object sender,RoutedEventArgs e)
{
if (!int.TryParse(tbQuestionCount.Text.Trim(),out totalQuestionCount))
{
tblStatus.Text = "Invalid question count!";
return;
}
correctAnswerCount = 0;
tblStatus.Text = string.Empty;
using(WpfQuizzyClient.QuizzyRef.QuizzyServiceClient client = new WpfQuizzyClient.QuizzyRef.QuizzyServiceClient("WSHttpBinding_IQuizzyService"))
{
newQuestion = client.NewGame(Convert.ToInt32(totalQuestionCount));
UpdateUserInterface(newQuestion);
}
}
private void btnAnswer_Click(object sender,RoutedEventArgs e)
{
Button button = (Button)sender;
int answerId = -1;
switch (button.Name)
{
case "btnAnswerA": answerId = 0; break;
case "btnAnswerB": answerId = 1; break;
case "btnAnswerC": answerId = 2; break;
case "btnAnswerD": answerId = 3; break;
default:
// This should never happen
MessageBox.Show("Invalid button name detected - contact support!");
return;
}
using (WpfQuizzyClient.QuizzyRef.QuizzyServiceClient client = new WpfQuizzyClient.QuizzyRef.QuizzyServiceClient("WSHttpBinding_IQuizzyService"))
{
if (answerId == client.CheckAnswer(newQuestion.Id,answerId))
{
tblStatus.Text = string.Empty;
tblStatus.Text = "Correct! Total: " + currentQuestionCount + " / " + totalQuestionCount;
currentQuestionCount += 1;
UpdateUserInterface(client.GetNextQuestion());
}
else
{
tblStatus.Text = string.Empty;
UpdateUserInterface(client.GetNextQuestion());
}
}
我的问题是,当我触发 btnAnswer_Click 时,它没有在 NewGame 中创建的列表,但我不允许执行诸如 NextQuestion(Question questionList) 之类的操作
如果您需要更多信息,请告诉我,我会确保尽快提供更多信息!
解决方法
我找到了修复......我只需要在 MainWindow() 之前删除 using 并添加“WpfQuizzyClient.QuizzyRef.QuizzyServiceClient client = new WpfQuizzyClient.QuizzyRef.QuizzyServiceClient("WSHttpBinding_IQuzzyService")”
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。