c# - 如何在一个函数中使用列表,该函数在另一个函数中添加了一些数据
问题描述
所以这是我的第一个文件 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() 之前删除并添加“WpfQuizzyClient.QuizzyRef.QuizzyServiceClient client = new WpfQuizzyClient.QuizzyRef.QuizzyServiceClient("WSHttpBinding_IQuizzyService")”
推荐阅读
- sql - System.TypeInitializationException:"Webservice.Helper.SqlHelper",System.NullReferenceException
- macos - 如何从 Mac 卸载 Appium 桌面及其缓存文件?
- powershell - Web App 无法创建提示“请求超时”
- html - 如何使内部带有网格的div居中?
- json - 在AngularJS中循环遍历包含数组的对象
- java - 如何延迟这个for循环
- python - 如何使用 C 中的 PortAudio 或 Python 中的 PyAudio 播放立体声
- android - 如何在不使用 Intent.ACTION_GET_CONTENT 的情况下将文件从媒体存储保存到 Firebase 存储
- c# - WebAPI 如何在受 Azure AD 保护的 web api 上允许 LocalHost 调试
- matlab - MATLAB 独立应用程序无法正常工作的原因可能是什么?