简体   繁体   中英

Why won't my List work?

The following is the code I've created thus far:

namespace Q_and_A
{
    public class question
    {        
        public string Question;
        public string Op1;
        public string Op2;
        public string Op3;
        public string Answer;

        public question(string questionString, string op1, string op2, string op3, string answer)
        {
            Question = questionString;
            Op1 = op1;
            Op2 = op2;
            Op3 = op3;
            Answer = answer;
        }
    }

    public class Questions
    {
        List<question> QuestionList = new List<question>();

        question Q1 = new question("Q", "?", "??", "???", "??");

        QuestionList.AddLast(Q1);

    }

    public partial class Form1 : Form
    {

    }
}

Errors generated by the "QuestionList.AddLast(Q1);" command line are as follows:

  1. Error 1 Invalid token '(' in class, struct, or interface member declaration C:\\TeenCoder\\MyProject\\Q and A Game\\Q and A Game\\Form1.cs 36 29 Q and A Game
  2. Error 2 Invalid token ')' in class, struct, or interface member declaration C:\\TeenCoder\\MyProject\\Q and A Game\\Q and A Game\\Form1.cs 36 32 Q and A Game
  3. Error 3 'Q_and_A_Game.Questions.QuestionList' is a 'field' but is used like a 'type' C:\\TeenCoder\\MyProject\\Q and A Game\\Q and A Game\\Form1.cs 36 9 Q and A Game
  4. Error 4 'Q_and_A_Game.Questions.Q1' is a 'field' but is used like a 'type' C:\\TeenCoder\\MyProject\\Q and A Game\\Q and A Game\\Form1.cs 36 30 Q and A Game

Details:

Ok. I feel rather stupid, but I can't figure this out! I'm learning my very first language, and I'm teaching myself (I'm homeschooled). I'm practicing what I've learned thus far (I'm using TeenCoder "Windows Programming") about LinkedLists and classes. The program I'm making is supposed to show a question with 3 answers. I created the "question" class without any errors or problems, then I created a LinkedList to hold all the questions the program would ask. Once again, no errors. Then I created a simple test question to add to the list. That's where the problem is. It wont let me use my LinkedList. I realize that I've probably made some stupidly simple mistake (like I said, I'm new to computer programming), but I cant figure it out. Thx :) ~ Ethan (This is my first question)

Edit: Thanks. I figured out my problem the day after I posted this question. I should have been creating a method in my "public partial class Form1 : Form" that would create my list of questions (if that makes any sense). Thanks for all the answers, help, and advice.

You are trying to execute statements that aren't contained inside a function. You probably want this:

public class Questions
{
    private List<question> QuestionList = new List<question>();

    public Questions()
    {
        question Q1 = new question("Q", "?", "??", "???", "??"); // just a test question

        QuestionList.Add(Q1); // This generates 4 errors

    }

}

Also, there is no AddLast. Items are automatically added to the end of a list by default. If you need to, you can use Insert instead of Add, if you wanted to insert an item at a specific spot in the list.

Also, just as an FYI, I'm not sure where you plan to go with this, but you can probably simplify your Questions class by inheriting it from List:

public class Questions : List<question>
{
}

This creates a strongly typed list, so you can do things like this:

Questions questions = new Questions();
questions.Add(new Question("Q", "?", "??", "???", "??"));

Before I think too hard about this one, fix the typo in that line ("QuesITonList" instead of "QuesTIonList") and see if the errors remain.

edit: More importantly, it looks like that is a syntax error. So bear with me because my C# is not great (it's similar in some ways to Java though): in most object-oriented languages, when you define a class, you can't really do much more than basic initialization ( SomeIntegerValue = 4 , for example) within that definition. Anything more complex, for example calling the AddLast() method, must be done within one of that class's methods.

It looks like you want AddLast(Q1) to be done during the initialization of that object. In such a situation, you should use a "constructor" method. Your question class has that method - it's the one where you set Question = question . Make a similar method for Questions , and put that line in there.

public class Questions
{
    List<question> QuestionList = new List<question>();

    //Inside Class, cannot call methods, you probably want these 2 in a constructor
    public Questions()
    {
        question Q1 = new question("Q", "?", "??", "???", "??"); // just a test question

        QuestionList.Add(Q1); // This generates 4 errors
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM