嘿所有我将一些C#代码转换为VB.net并注意到C#代码有一个 Public Sub New()。 C#代码是这样的:
private readonly SController _controller;
public SView():this(new SController()) { }
public SView(SController controller)
{
InitializeComponent();
_controller = controller;
controller.EnumerateDevices(deviceDropdown.Items);
_thread = new Thread(ReadLoop) {IsBackground = true};
_thread.Start();
}
将上述内容转换为VB.net:
Private ReadOnly _controller As SController
Public Sub New()
Me.New(New SController())
End Sub
Public Sub New(controller As SController)
InitializeComponent()
_controller = controller
controller.EnumerateDevices(deviceDropdown.Items)
_thread = New Thread(AddressOf ReadLoop) With { _
.IsBackground = True _
}
_thread.Start()
End Sub
我遇到的问题是,因为VB以:
开头Private Sub SView_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
如何判断它启动 Public Sub New()功能?
我试过放:
Private Sub SView_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.New(New SController())
End Sub
但它给了我错误:
Constructor call is valid only as the first statement in an instance constructor.
任何帮助都可以解决这个问题!
更新
Private Sub SView_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim _sView As New SView
End Sub
当我在线上休息时,它永远不会达到负荷 Dim _sView As New SView
答案 0 :(得分:1)
如何判断它是否触发Public Sub New()函数?
你不能。 Sub New()
是类的构造函数,用于初始化类的实例。在创建对象后,您无法调用它。
答案 1 :(得分:1)
在实例化表单并在Load事件触发之前调用默认构造函数(没有任何参数的构造函数)。如果要验证这一点,请在构造函数中放置一个断点并调试解决方案。
答案 2 :(得分:1)
Sub New
“Sub 新”===> “Dim foo As 新 FooBar”得到它?
有些混淆可能是因为你的SView构造函数被重载了:
' simple constructor
Public Sub New()
Me.New(New SController()) ' calls overload below
End Sub
Public Sub New(controller As SController)
InitializeComponent() ' this is apparently a form????
_controller = controller
controller.EnumerateDevices(deviceDropdown.Items)
_thread = New Thread(AddressOf ReadLoop) With { _
.IsBackground = True }
_thread.Start()
End Sub
可以在有或没有对控制器的引用的情况下调用它:
Dim _sView As New SView
' or
Dim _ctrlr As New SController
Dim _sView As New SView(_ctrlr)
在第一种情况下,您的SView将创建自己的新控制器。在第二个中,它将使用在构造函数调用(New SView(_ctrlr)
)中传递给它的那个。在两个情况下,您的新对象将具有控制器对象。
如果没有ctor arg不存在新的类对象,只需删除简单(空)构造函数,以便抛出异常:
Dim emp As New Employee(empName)
' this one fails if you remove the "Sub New()":
Dim emp As New Employee()
如果您只想使用您创建的控制器创建新实例,只需删除simple / empty构造函数。
修改强>
It never hits the Load when I put a break on line [Dim _sView As New SView]
加载表单与创建表单实例不同。表单只是类,而Load事件在对象上调用Show
方法时使用第一次。这是一个好的东西,因为它允许我们创建一个表单实例并在显示之前使用 。更长的符号:
Dim _sView As SView ' simple DECLARES the object name and type
_sView = New SView ' create instance (exec [Sub new])
_sView.FormSetup ' do some first time only stuff
_sView.Show ' show the form (fires [Load] event)
FormSetup
可能是我编写的一种方法,这是一些只在第一次使用时才能完成的任务。如果我随后只是Hide
表单,我可以跳过所有这些,而不是在Load
事件中。然而,评论解释了每一行的最新情况。
答案 3 :(得分:0)
如果你想在已经构造的实例上调用构造函数中的逻辑,你需要重构以将该逻辑移出构造函数:
Private ReadOnly _controller As SController
Public Sub New()
Me.New(New SController())
End Sub
Public Sub New(controller As SController)
Initialize(controller)
End Sub
Private Sub Initialize(controller As SController)
InitializeComponent()
_controller = controller
controller.EnumerateDevices(deviceDropdown.Items)
_thread = New Thread(AddressOf ReadLoop) With { _
.IsBackground = True _
}
_thread.Start()
End Sub
然后,您可以拨打Me.New(New SController())
Me.Initialize(New SController())