2016-11-16 12 views
1

C#でアプリケーションを処理していて、予期せずこの例外が発生しました。 "Slate.exeで 'System.StackOverflowException'型の未処理例外が発生しました。 "Slate.exeで 'System.StackOverflowException'型の未処理例外が発生しました

以下は私のコードの詳細です。

これは、私がメインクラスのオブジェクトを初期化したクラスです。

public class MenuControls 
{ 
    MainWindow MainWindowObj = new MainWindow(); //This thing is raising Exception. 

    public void SaveAs() 
    { 

     SaveFileDialog SFD = new SaveFileDialog(); 
     if (SFD.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
     { 

      String FPath = SFD.FileName; 
      StreamWriter SWriter = new StreamWriter(File.Create(FPath)); 
      SWriter.Write(MainWindowObj.PlayGround.Text); 
      SWriter.Dispose(); 

     } 
    } 

    public void NewFile() 
    { 
     MessageBox.Show("You haven't saved your file yet ! Do you want to create new file?", "Something", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning); 
    } 

    public void OpenFile() 
    { 
     OpenFileDialog OFD = new OpenFileDialog(); 

     if (OFD.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
     { 
      StreamReader SReader = new StreamReader(File.OpenRead(OFD.FileName)); 
      MainWindowObj.PlayGround.Text = SReader.ReadToEnd(); 
     } 
    } 
} 

私のメインクラスのコードです。ここで私のオブジェクトMenuConrolsクラスを初期化しました。

public partial class MainWindow : Form 
{ 
    public RichTextBox GetBox() 
    { 
     return PlayGround; 
    } 
    MenuControls Menu = new MenuControls(); 
    TextEditor_Preferences Prefrences = new TextEditor_Preferences(); 

    public int SaveStatus = 0; 
    TextEditor_Preferences PreferencesForm = new TextEditor_Preferences(); 
    public MainWindow() 
    { 

     InitializeComponent(); 

    } 

    private void MainMenuFile_SaveAs_Click(object sender, EventArgs e) 
    { 
     Menu.SaveAs(); 
     SaveStatus = 1; 
    } 

    private void MainMenuFile_New_Click(object sender, EventArgs e) 
    { 
     if (SaveStatus == 0 && PlayGround.Text.Contains(" ")) 
     { 
      Menu.NewFile(); 
     } else 
     { 
      PlayGround.Text = ""; 
     } 

    } 

    private void MainMenuFile_Open_Click(object sender, EventArgs e) 
    { 
     Menu.OpenFile(); 
    } 

    private void MainWindow_KeyDown(object sender, KeyEventArgs e) 
    { 
     if (e.Control && e.KeyCode.ToString() == "N") 
     { 
      if (SaveStatus == 0 && !String.IsNullOrWhiteSpace(PlayGround.Text)) 
      { 
       DialogResult DResult = MessageBox.Show("You haven't saved your file yet ! Do you want to create new file?", "Something", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning); 
       if (DResult == DialogResult.Yes) 
       { 
        PlayGround.Text = ""; 
       } else 
       { 

       } 
      } 
      else 
      { 
       PlayGround.Text = ""; 
      } 
     } 
     else if (e.Control && e.KeyCode.ToString() == "O") 
     { 
      Menu.OpenFile(); 
     } 
     else if (e.Control && e.KeyCode.ToString() == "S") 
     { 
      Menu.SaveAs(); 
      SaveStatus = 1; 
     } 
    } 
    private void MainMenuEdit_Preferences_Click(object sender, EventArgs e) 
    { 
     PreferencesForm.ShowDialog(); 
    } 

} 

私に解決策を提案してください。 何か不足している場合、または質問に適切な質問がない場合は、明示的にお知らせください。 ありがとうございました。あなたが実行さばかり終わることはありませんコードを持っている、これは

+2

あなたはどこかで再帰的なメソッド呼び出しを持っています。スタックトレースを確認すると、すぐに見つけることができます。 – dymanoid

+3

MainWindowのすべてのインスタンスがMenuControlsのインスタンスを作成します。 MenuControlsのすべてのインスタンスは、MainWindowのインスタンスを作成しました。 - >無限再帰 – NineBerry

+0

@dymanoid StackOverflowExceptionsは、通常、マネージデバッグを伴うスタックトレースを含みません。 – vcsjones

答えて

4

、あなたがMenuControlsクラスでMainWindowのインスタンスをインスタンス化されています

public class MenuControls 
{ 
    MainWindow MainWindowObj = new MainWindow(); // note this 

MainWindowクラスで、あなたはinstatiatedなっている他のフィールドMenuControlsを持っています、したがって、MainWindowインスタンスを作成すると、MenuControlsフィールドがinstatntateされます。フィールドは、それが持つMainWindowフィールドを再度埋め込みます。これにより、その結果、stackoverflow例外が発生します。あなたがMenuControlsクラスまたはその逆のいずれかからMainWindowフィールドを削除する必要が

public partial class MainWindow : Form 
{ 
    public RichTextBox GetBox() 
    { 
     return PlayGround; 
    } 
    MenuControls Menu = new MenuControls(); // note this 

、どのような私が見るものはあなたがそこからそれを削除し、MenuControlクラスでMainWindowフィールドを必要としないことです。

+0

私はMenuControlsで使用する必要があるリッチテキストボックスを持っています。なぜこれを行ったのですか? –

+0

richtextboxオブジェクトをパラメータとしてcontrcutorまたはMenuControlsクラスで必要なメソッドに渡すことができます –

+0

私はそれを試してみましょう.. –

関連する問題