2016-12-28 51 views
-3

我對c#和編程一般都很陌生,並且試圖在Windows窗體中構建一種測驗,其中問題,答案和選擇是存儲在列表中,問題,答案和選擇在單獨的類中。 Windows窗體部分是四個按鈕和一些文本框,可讓您選擇要選擇的選項(1-4)。所有使用的列表都是字符串類型btw。 但是,每當我嘗試啓動該程序時,都會被告知TypeInitializer引發異常。 InnerException是索引超出範圍。 HRESULT的是-2146233036,並且如果它的任何幫助,整個堆棧跟蹤是:當試圖啓動程序時,內部異常「索引超出界限」

粘彈性阻尼器quizforreal.Display..ctor() 粘彈性阻尼器quizforreal.Program.Main()I C:\用戶\安德斯\文件\ 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.Executio nContext.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回調,對象狀態) 粘彈性阻尼器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()); 
     } 
    } 
} 

希望你們能幫助:)在此先感謝!

+4

現在好了,如何赫克你認爲我們可以幫助你找到代碼中的bug,如果沒有代碼,我們看到了什麼?這將如何工作? –

+0

您正嘗試訪問不存在的列表中的元素。調試以找出在哪裏。 – Carcigenicate

+0

哎呦,對不起那傢伙 - 我可以發誓我加了 – sunero4

回答

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)"); 
     ... 
+0

非常感謝!不知道我錯過了什麼,我是在所有的建設者中完成的。我想我有時會混淆數組和列表。它現在開始,所以非常感謝你:) – sunero4