表单在从默认实例构建期间引用自身,这导致了无限递归 [英] The form referred to itself during construction from a default instance, which led to infinite recursion

查看:67
本文介绍了表单在从默认实例构建期间引用自身,这导致了无限递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用带有图形用户界面的 QuickFix 创建一个简单的客户端(启动器).我正在使用 Visual Studio 2012 并在 VB.Net 中编程.

I am currently trying to create a simple client (Initiator) using QuickFix with a graphical user interface. I'm using Visual Studio 2012 and programming in VB.Net.

这是我的问题:当我启动我的应用程序时,出现以下错误:WindowsApplication1.exe 中发生了类型为‘System.InvalidOperationException’的未处理异常

Here is my problem : When I launch my app, I have this error : "An unhandled exception of type 'System.InvalidOperationException' occurred in WindowsApplication1.exe

附加信息:创建表单时出错.有关详细信息,请参阅 Exception.InnerException.错误是:表单在从默认实例构造期间引用自身,导致无限递归.在 Form 的构造函数中,使用 'Me' 引用该表单."

Additional information: An error occurred creating the form. See Exception.InnerException for details. The error is: The form referred to itself during construction from a default instance, which led to infinite recursion. Within the Form's constructor refer to the form using 'Me.'"

我的项目中有两个文件,它们是 Client GUI.vb (http://pastebin.com/virgVNyS) 和 MyQuickFixApp.vb(http://pastebin.com/tQ1GXNSx).第二个包含将 IApplication 与所有子程序集成的类.

I have two files in my project which are Client GUI.vb (http://pastebin.com/virgVNyS) and MyQuickFixApp.vb (http://pastebin.com/tQ1GXNSx). The second one contains the class that integrates the IApplication, with all the subs.

执行此行时发生错误:Dim Initiator As New SocketInitiator(myApp, storeFactory, settings, logFactory)" from Client GUI.vb但该软件突出显示了文件 Application.Designer.vb 中的一行:

The error happens when it executes this line : "Dim initiator As New SocketInitiator(myApp, storeFactory, settings, logFactory)" from Client GUI.vb but the software highlights a line from the file Application.Designer.vb which is :

Protected Overrides Sub OnCreateMainForm()
    Me.MainForm = Global.WindowsApplication1.ClientGUI
End Sub

你能帮我告诉我有什么问题吗?

Can you help me and tell me what is wrong ?

非常感谢!

推荐答案

在处理 WinForms 时,避免出现问题的最佳方法是初始化所有内容(除了简单的变量赋值,例如:Dim filename As String = "initiator.cfg" 很好)在构建/加载 GUI 之后(在 _Load 方法上).您收到此错误的原因是因为在实际创建之前引用了主表单 (Me.MainForm =).

When dealing with WinForms, the best proceeding to avoid problems is initialise everything (except simple variable assignation, for example: Dim filename As String = "initiator.cfg" is fine) after the GUI has been constructed/loaded (on the _Load method). The reason why you are getting this error is because of referring to the main Form (Me.MainForm =) before it has been actually created.

Dim Initiator As New SocketInitiator(myApp, storeFactory, settings, logFactory) 移动到 ClientGUI_Load(主窗体的 Load Event 方法)),错误就会消失.

Move Dim initiator As New SocketInitiator(myApp, storeFactory, settings, logFactory) to ClientGUI_Load (the Load Event method of your main form) and the error will disappear.

注意:如果你想从任何地方"访问initiator,你应该保留全局声明但将赋值移动到Load事件,即:

NOTE: if you want to access initiator from "anywhere", you should keep the global declaration but move the assignation to the Load event, that is:

Dim initiator As SocketInitiator 'at the Class level, outside any sub/function (as previously)

initiator = New SocketInitiator(myApp, storeFactory, settings, logFactory) 'Inside the ClientGUI_Load method. 

这篇关于表单在从默认实例构建期间引用自身,这导致了无限递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆