Question

HOMEWORK QUESTION: I need to create a simple trivia game that reads from a CSV file. My data for a particular question is structured as follows: "Question;AnswerA;AnswerB;AnswerC;AnswerD;CorrectAnswerLetter".

We're using a series of getters and setters to hold all the relevant data for a single question object, and I'm running into a problem with the array I've created to hold the four answers.

In my constructor, I'm using this code--which I believe instantiates the Answer array in question:

class TriviaQuestionUnit
{
    ...

    const int NUM_ANSWERS = 4;
    string[] m_Answers = new String[NUM_ANSWERS];

    public string[] Answer
    {
        get { return m_Answers[]; }
        set { m_Answers = value[];
    }

    ...

    // Answer array
    public string[] GETAnswer(int index) 
    {
        return m_Questions[index].Answer;
    }

    ...
}

I'm accessing the getter and setter from my TriviaQuestionBank method, which includes this code:

...

const int NUM_QUESTIONS = 15;
TriviaQuestionUnit[] m_Questions = new TriviaQuestionUnit[NUM_QUESTIONS];

...

// Answer array
public string[] GETAnswer(int index) 
{
    return m_Questions[index].Answer;
}

...

I'm using using StreamReader to read a line of input from my file

...

char delim = ';'; 
String[] inputValues = inputText.Split(delim); 

...

parses the input in an array from which I create the question data. For my four answers, index 1 through 4 in the inputValues array, I populate this question's array with four answers.

...

for (int i = 0; i < NUM_ANSWERS; i++)                                      
{
    m_Questions[questionCounter].Answer[i] = inputValues[i + 1];    
}

...

I'm getting errors of Syntax code, value expected on the getters/setters in my constructor, and if I change the variable to m_Answers[NUM_QUESTIONS] I get an error that I can't implicitly convert string to String[].

Hopefully I've posted enough code for someone to help point me in the right direction. I feel like I'm missing something obvious, but I just cannot make this work.

Was it helpful?

Solution

Your code has some errors that will cause compilation errors, so my first lesson for you is going to be: listen to the compiler. Some of the errors might seem a bit hard to understand sometimes, but I can ensure you that a lot of other people have had the same problems before; googling a compiler error often gives you examples from other people that are similar to your issue.

You say "In my constructor", but the problem is that your code does not have a constructor. You do however initialize fields and properties on your class and surely enough, the compiler will create a default constructor for you, but you have not defined one yourself. I am not saying that your code does not work because you do not have a constructor, but you might be using the wrong terms.

The first problem is in your first code snippet inside TriviaQuestionUnit. Your first two lines are working correctly, you are creating a constant integer with the value 4 that you use to determine how large your array is going to be and then you initialize the array with that given number.

When you do new string[NUM_ANSWERS] this will create an array, with default (empty) values.

The first problem that arises in your code is the getters and setters. The property expects you to return an array of strings which the method signature in fact is telling us:

public string[] Answer

However, looking at the getter and setter, what is it that you return?

m_Answers is a "reference" to your array, hence that whenever you write m_Answers you are referring to that array. So what happens when we add the square brackets?

Adding [] after the variable name of an array indicates that we want to retrieve a value from within the array. This is called the indexer, we supply it with an index of where we want to retrieve the value from within the array (first value starts at index 0). However, you don't supply it with a value? So what is returned?

Listen to the compiler!

Indexer has 1 parameter(s) but is invoked with (0) argument(s)

What does this tell you? It tells you that it doesn't expect the empty [] but it would expect you to supply the indexer with a number, for instance 0 like this: [0]. The problem with doing that here though, is that this would be a miss-match to the method signature.

So what is it that we want?

We simply want to return the array that we created, so just remove [] and return m_Answers directly like this:

public string[] Answer
{
    get { return m_Answers; }
    set { m_Answers = value; }
}

Note that you were also missing a curly bracket at the end if the set.

When fixing this, there might be more issues in your code, but trust the compiler and try to listen to it!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top