简体   繁体   English

我如何在一个 function 中使用该列表,该列表在另一个 function 中添加了一些数据

[英]How can i use the list in one function which has some data added in another function

So here is my first file QuizzyService:所以这是我的第一个文件 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];
    }

Here the relevant part of the xaml.cs file:这里是 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());
            }
        }

My problem is that when i trigger btnAnswer_Click it doesnt have the list which has been created in NewGame but im not allowed to do something like NextQuestion(Question questionList)我的问题是,当我触发 btnAnswer_Click 时,它没有在 NewGame 中创建的列表,但我不允许执行 NextQuestion(Question questionList) 之类的操作

If you need more info just tell me and i'll make sure to provide more as quickly as possible!如果您需要更多信息,请告诉我,我会确保尽快提供更多信息!

i found the fix... i just had to remove both using and add the "WpfQuizzyClient.QuizzyRef.QuizzyServiceClient client = new WpfQuizzyClient.QuizzyRef.QuizzyServiceClient("WSHttpBinding_IQuizzyService")" before the MainWindow()我找到了修复...我只需要在 MainWindow() 之前删除并添加“WpfQuizzyClient.QuizzyRef.QuizzyServiceClient client = new WpfQuizzyClient.QuizzyRef.QuizzyServiceClient(“WSHttpBinding_IQuizzyService”)”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 任务在添加到列表后立即调用<task>如果 function 有括号。 如何将 args 作为任务传递给 function?</task> - Task invokes immediately upon being added to a List<Task> if the function has parenthesis. How can I pass in args to a function as a Task? 如何仅将一个属于两个对象的函数用于一个对象? - How can I use one function, which belongs to two objects, for only one objects? 如何编写一个函数来调用另一个函数并捕获一些异常? - How to write a function which will call another function and catch some exceptions? 如何制作一个遍历列表并以要访问的数据成员作为输入参数的函数 - How can I make a function that loops over a list and takes which data member to access as input parameter 如何在 function 中使用列表中的相同数据? - How can I use the same data from my list inside my function? 如何使用ScriptManager的多个功能? - How can I use ScriptManager more than one function? 如何在 lambda function c# 的位置中使用列表? - How can I use a list in Where of lambda function c#? 我如何在 class 中的一个方法中使用另一个方法中已更改的布尔值? - How can I use a bool value in class in a method which has changed in another method? 如何为具有带参数运行方法的 azure 函数编写单元测试? - How can I write unit test for azure function which has run method with parameter? 我可以使用其他一些类的函数作为委托吗? - Can I use some other class's function as a delegate?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM