当尝试启动程序时,内部异常“索引超出范围......”

时间:2016-12-28 15:58:33

标签: c#

我对c#和一般编程很新,并试图在Windows窗体中构建一种测验,其中问题,答案和选择存储在列表中,而问题,答案和选择都在不同的类中。 Windows窗体部分是四个按钮和一些文本框,可让您选择要选择的选项(1-4)。使用的所有列表都是字符串类型btw。 但每当我尝试启动程序时,我都被告知TypeInitializer引发了异常。 InnerException是索引超出范围。 HResult是-2146233036,如果有任何帮助,整个StackTrace是:

  

ved quizforreal.Display..ctor()      ved quizforreal.Program.Main()i C:\ Users \ Anders \ documents \ visual studio 2015 \ Projects \ quizforreal \ quizforreal \ Program.cs:linje 19      ved System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,String [] args)      ved System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)      ved Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()      ved System.Threading.ThreadHelper.ThreadStart_Context(Object state)      ved System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean preserveSyncCtx)      ved System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean preserveSyncCtx)      ved System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)      ved System.Threading.ThreadHelper.ThreadStart()

以下是代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace quizforreal
{
public partial class Display : Form
{
    int tæller = 0;
    private static Choices choiceobj = new Choices();
    private static Questions questobj = new Questions();
    private static Answers answerobj = new Answers();
    List<string> choices1list = choiceobj.getChoiceOne();
    List<string> choices2list = choiceobj.getChoiceTwo();
    List<string> choices3list = choiceobj.getChoiceThree();
    List<string> choices4list = choiceobj.getChoiceFour();
    List<string> questlist = questobj.getQuestions();
    List<string> answerlist = answerobj.getAnswers();

    public Display()
    {
        InitializeComponent();        
}

    public void skifter()
    {


        switch (tæller)
        {
            case 0:
                choice1.Text = choices1list[0];
                choice2.Text = choices2list[0];
                choice3.Text = choices3list[0];
                choice4.Text = choices4list[0];
                quest.Text = questlist[0];
                break;
            case 1:
                choice1.Text = choices1list[1];
                choice2.Text = choices2list[1];
                choice3.Text = choices3list[1];
                choice4.Text = choices4list[1];
                quest.Text = questlist[1];
                break;
            case 2:
                choice1.Text = choices1list[2];
                choice2.Text = choices2list[2];
                choice3.Text = choices3list[2];
                choice4.Text = choices4list[2];
                quest.Text = questlist[2];
                break;
            case 3:
                choice1.Text = choices1list[3];
                choice2.Text = choices2list[3];
                choice3.Text = choices3list[3];
                choice4.Text = choices4list[3];
                quest.Text = questlist[3];
                break;
            case 4:
                choice1.Text = choices1list[4];
                choice2.Text = choices2list[4];
                choice3.Text = choices3list[4];
                choice4.Text = choices4list[4];
                quest.Text = questlist[4];
                break;
        }
    }


    private void choice1_TextChanged(object sender, EventArgs e)
    {

    }

    private void choice2_TextChanged(object sender, EventArgs e)
    {

    }

    private void choice3_TextChanged(object sender, EventArgs e)
    {

    }

    private void choice4_TextChanged(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        switch (tæller)
        {
            case 0:
                if (choice1.Text == answerlist[0])
                {
                    tæller++;
                }
                else
                {
                    tæller = 0;
                }
                break;
            case 1:
                if (choice1.Text == answerlist[0])
                {
                    tæller++;
                }
                else
                {
                    tæller = 0;
                }
                break;
            case 2:
                if (choice1.Text == answerlist[0])
                {
                    tæller++;
                }
                else
                {
                    tæller = 0;
                }
                break;
            case 3:
                if (choice1.Text == answerlist[0])
                {
                    tæller++;
                }
                else
                {
                    tæller = 0;
                }
                break;
            case 4:
                if (choice1.Text == answerlist[0])
                {
                    tæller++;
                }
                else
                {
                    tæller = 0;
                }
                break;
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        switch (tæller)
        {
            case 0:
                if (choice2.Text == answerlist[0])
                {
                    tæller++;
                }
                else
                {
                    tæller = 0;
                }
                break;
            case 1:
                if (choice2.Text == answerlist[0])
                {
                    tæller++;
                }
                else
                {
                    tæller = 0;
                }
                break;
            case 2:
                if (choice2.Text == answerlist[0])
                {
                    tæller++;
                }
                else
                {
                    tæller = 0;
                }
                break;
            case 3:
                if (choice2.Text == answerlist[0])
                {
                    tæller++;
                }
                else
                {
                    tæller = 0;
                }
                break;
            case 4:
                if (choice2.Text == answerlist[0])
                {
                    tæller++;
                }
                else
                {
                    tæller = 0;
                }
                break;
        }
    }

    private void button3_Click(object sender, EventArgs e)
    {
        switch (tæller)
        {
            case 0:
                if (choice3.Text == answerlist[0])
                {
                    tæller++;
                }
                else
                {
                    tæller = 0;
                }
                break;
            case 1:
                if (choice3.Text == answerlist[0])
                {
                    tæller++;
                }
                else
                {
                    tæller = 0;
                }
                break;
            case 2:
                if (choice3.Text == answerlist[0])
                {
                    tæller++;
                }
                else
                {
                    tæller = 0;
                }
                break;
            case 3:
                if (choice3.Text == answerlist[0])
                {
                    tæller++;
                }
                else
                {
                    tæller = 0;
                }
                break;
            case 4:
                if (choice3.Text == answerlist[0])
                {
                    tæller++;
                }
                else
                {
                    tæller = 0;
                }
                break;
        }
    }

    private void button4_Click(object sender, EventArgs e)
    {
        switch (tæller)
        {
            case 0:
                if (choice4.Text == answerlist[0])
                {
                    tæller++;
                }
                else
                {
                    tæller = 0;
                }
                break;
            case 1:
                if (choice4.Text == answerlist[0])
                {
                    tæller++;
                }
                else
                {
                    tæller = 0;
                }
                break;
            case 2:
                if (choice4.Text == answerlist[0])
                {
                    tæller++;
                }
                else
                {
                    tæller = 0;
                }
                break;
            case 3:
                if (choice4.Text == answerlist[0])
                {
                    tæller++;
                }
                else
                {
                    tæller = 0;
                }
                break;
            case 4:
                if (choice4.Text == answerlist[0])
                {
                    tæller++;
                }
                else
                {
                    tæller = 0;
                }
                break;
        }
    }
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace quizforreal
{
public class Choices
{
    private List<string> choice1list = new List<string>();
    private List<string> choice2list = new List<string>();
    private List<string> choice3list = new List<string>();
    private List<string> choice4list = new List<string>();


    public Choices()
    {
        choice1list[0] = "mulighed1(1)";
        choice1list[1] = "mulighed1(2)";
        choice1list[2] = "mulighed1(3)";
        choice1list[3] = "mulighed1(4)";
        choice1list[4] = "mulighed1(5)";

        choice2list[0] = "mulighed2(1)";
        choice2list[1] = "mulighed2(2)";
        choice2list[2] = "mulighed2(3)";
        choice2list[3] = "mulighed2(4)";
        choice2list[4] = "mulighed2(5)";

        choice3list[0] = "mulighed3(1)";
        choice3list[1] = "mulighed3(2)";
        choice3list[2] = "mulighed3(3)";
        choice3list[3] = "mulighed3(4)";
        choice3list[4] = "mulighed3(5)";

        choice4list[0] = "mulighed4(1)";
        choice4list[1] = "mulighed4(2)";
        choice4list[2] = "mulighed4(3)";
        choice4list[3] = "mulighed4(4)";
        choice4list[4] = "mulighed4(5)";

    }

    public List<string> getChoiceOne()
    {
        return choice1list;
    }

    public List<string> getChoiceTwo()
    {
        return choice2list;
    }

    public List<string> getChoiceThree()
    {
        return choice3list;
    }

    public List<string> getChoiceFour()
    {
        return choice4list;
    }

}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace quizforreal
{
    public class Questions
    {
        private List<string> questionslist = new List<string>();


        public Questions()
        {
            questionslist[0] = "Spørgsmål 1";
            questionslist[1] = "Spørgsmål 2";
            questionslist[2] = "Spørgsmål 3";
            questionslist[3] = "Spørgsmål 4";
            questionslist[4] = "Spørgsmål 5";
        }


        public List<string> getQuestions()
        {
            return questionslist;
        }


    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace quizforreal
{
    class Answers
    {
        private static Choices choicesobj = new Choices();
        List<string> choices1list = choicesobj.getChoiceOne();
        List<string> choices2list = choicesobj.getChoiceTwo();
        List<string> choices3list = choicesobj.getChoiceThree();
        List<string> choices4list = choicesobj.getChoiceFour();

        private List<string> answerlist = new List<string>();


        public Answers()
        {
            answerlist[0] = choices1list[0];
            answerlist[1] = choices3list[1];
            answerlist[2] = choices4list[2];
            answerlist[3] = choices1list[3];
            answerlist[4] = choices2list[4];

        }


        public List<string> getAnswers()
        {
            return answerlist;
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace quizforreal
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Display());
        }
    }
}

希望你们能帮忙:)提前致谢!

1 个答案:

答案 0 :(得分:0)

“TypeInitializer抛出异常”表示静态构造函数或字段初始值设定项中存在错误。您的表单类有一个静态字段private static Choices choiceobj = new Choices();查看Choices的构造函数,我看到您正在尝试使用索引器将项“添加”到列表中,如果列表将引发异常并不是那么大,所以你应该使用Add而不是索引器:

public class Choices
{
    private List<string> choice1list = new List<string>();
    ...

    public Choices()
    {
        choice1list.Add("mulighed1(1)");
        choice1list.Add("mulighed1(2)");
        ...