在“Dim mdi as new MDIParent1”中遇到异常在VB.NET中

发布于 2024-12-20 21:47:46 字数 1135 浏览 2 评论 0原文

我在这段代码中遇到异常:

Imports System.Windows.Forms
Imports System.Text
Imports System.Diagnostics

Public Class MDIParent1
   Private Sub MDIParent1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles  MyBase.Load
      getapp.getApplication()
   End Sub
   Public Sub MDIParent1(ByVal value As String, ByVal value1 As String)
      Dim ChildForm As New System.Windows.Forms.Form
      ChildForm.MdiParent = Me
      ChildForm.Text = value1
      ChildForm.Show()
   End Sub
End Class

Public Class getApplications

   Dim w As String
   Dim b As New Process()
   Dim p As String
   Dim mdi As New MDIParent1   'here i am getting exception that is System.StackOverflowException was unhandled   InnerException:..
   Dim i As Integer
   Public Sub getApplication()

      For Each Me.b In Process.GetProcesses(".")
         Try
            If b.MainWindowTitle.Length > 0 Then
               p = b.ProcessName.ToString()
               w = b.MainWindowTitle().ToString()
               mdi.MDIParent1(p, w)
            End If
         Catch
         End Try
      Next
   End Sub
End Class

I'm getting an exception in this code:

Imports System.Windows.Forms
Imports System.Text
Imports System.Diagnostics

Public Class MDIParent1
   Private Sub MDIParent1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles  MyBase.Load
      getapp.getApplication()
   End Sub
   Public Sub MDIParent1(ByVal value As String, ByVal value1 As String)
      Dim ChildForm As New System.Windows.Forms.Form
      ChildForm.MdiParent = Me
      ChildForm.Text = value1
      ChildForm.Show()
   End Sub
End Class

Public Class getApplications

   Dim w As String
   Dim b As New Process()
   Dim p As String
   Dim mdi As New MDIParent1   'here i am getting exception that is System.StackOverflowException was unhandled   InnerException:..
   Dim i As Integer
   Public Sub getApplication()

      For Each Me.b In Process.GetProcesses(".")
         Try
            If b.MainWindowTitle.Length > 0 Then
               p = b.ProcessName.ToString()
               w = b.MainWindowTitle().ToString()
               mdi.MDIParent1(p, w)
            End If
         Catch
         End Try
      Next
   End Sub
End Class

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

醉殇 2024-12-27 21:47:46

问题是你有一个永远不会结束的递归调用。

MDIParent1.MDIParent1() 调用 getApplications.getApplication(),
哪个调用
MDIParent1.MDIParent1(),再次调用 getApplications.getApplication(),
哪个调用
MDIParent1.MDIParent1(),再次调用 getApplications.getApplication(),
哪个调用
MDIParent1.MDIParent1(),再次调用 getApplications.getApplication(),
哪个调用
MDIParent1.MDIParent1(),再次调用 getApplications.getApplication(),
哪个调用
MDIParent1.MDIParent1(),再次调用 getApplications.getApplication(),
哪个调用
MDIParent1.MDIParent1(),它再次调用 getApplications.getApplication(),

依此类推直至无穷大(或直到系统抛出 StackOverflowException)。

The problem is that you have a recursive call that never ends.

MDIParent1.MDIParent1() calls getApplications.getApplication(),
which calls
MDIParent1.MDIParent1(), which again calls getApplications.getApplication(),
which calls
MDIParent1.MDIParent1(), which again calls getApplications.getApplication(),
which calls
MDIParent1.MDIParent1(), which again calls getApplications.getApplication(),
which calls
MDIParent1.MDIParent1(), which again calls getApplications.getApplication(),
which calls
MDIParent1.MDIParent1(), which again calls getApplications.getApplication(),
which calls
MDIParent1.MDIParent1(), which again calls getApplications.getApplication(),

and so on into infinity (or until the system throws a StackOverflowException).

泅人 2024-12-27 21:47:46

当您创建 MDIParent1 实例时,您将调用 getApplication()。当您调用 getApplication() 时,您将创建一个 MDIParent1 的新实例。这是无限递归导致堆栈溢出异常。

您实际上尝试使用这段代码做什么?

When you create an instance of MDIParent1 you call getApplication(). When you call getApplication() you create a new instance of MDIParent1. It's an infinite recursion resulting in a stack overflow exception.

What are you actually trying to do with this code?

笛声青案梦长安 2024-12-27 21:47:46

如果将 New 与 Dim 分开分配,您会得到相同的异常吗?

Dim variable as DataType
// ...

Public Sub InitStuff()

    Set variable = New DataType

Do you get the same Exception if you assign New separately from Dim?

Dim variable as DataType
// ...

Public Sub InitStuff()

    Set variable = New DataType
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文