.NET MAUI Call List inside List

Anonymous
2023-12-09T14:37:50.1533333+00:00

Here is my quiz application currently I am doing a string match to determine if the answer if correct or not.

    if (option.Trim() == Questions[questionIndex].Answer.Trim())
            {
                score++;
            }
            else
            {
                failedQuestions[failedIndex] = Questions[questionIndex].QuestionTitle + " " + Questions[questionIndex].Answer;
                failedIndex++;


            }


I added new field Correct, which points to index of the answer

I want to replace this

if (option.Trim() == Questions[questionIndex].Answer.Trim())

with

if (option.Trim() == Questions[questionIndex].Options[Questions[questionIndex].Correct])

but this if statement is giving error

Here is github repository, please suggest.

https://sheet2api.com/v1/wlS0h0USxm9p/quiz

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,665 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
4,008 questions
0 comments No comments
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 33,161 Reputation points Microsoft External Staff
    2023-12-11T02:41:05.4033333+00:00

    Hi @Maui Learner

    To check the answer based on the answer index, first, you should according to the question index to find all answers and then find the correct answer index, after that compare it with the Questions[questionIndex].Correct.

    So, you can modify the condition statement as below:

            protected void OptionSelected(string option)
            {  
                //check the answer based on text.
                //if (option.Trim() == Questions[questionIndex].Answer.Trim()) 
    
                //check the answer based on answer index.
                // var anserindex = Questions[questionIndex].Options.ToList().IndexOf(option.Trim()) + 1; //: find the answer index from the options,
                // var correctindex = Questions[questionIndex].Correct;  // get the correct answer from the Questions
                if ((Questions[questionIndex].Options.ToList().IndexOf(option.Trim()) + 1) == Questions[questionIndex].Correct)
                {
                    score++;
                }
                else
                {
                    failedQuestions[failedIndex] = Questions[questionIndex].QuestionTitle + " " + Questions[questionIndex].Answer;
                    failedIndex++; 
                }
                questionIndex++;
            }
    

    After that, the result like this:

    1


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.