VB.net 中的 Public Sub New() 触发函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23748414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Public Sub New() firing function in VB.net
提问by StealthRT
Hey all I am converting some C# code to VB.net and noticed that the C# code has a Public Sub New(). The C# code is this:
大家好,我正在将一些 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();
}
And converting the above to VB.net:
并将上述内容转换为 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
The problem I am running into is that since VB starts with:
我遇到的问题是,由于 VB 以:
Private Sub SView_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
How can I tell it to fire off the Public Sub New()function?
我怎样才能告诉它触发Public Sub New()函数?
I tried just putting:
我试着把:
Private Sub SView_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.New(New SController())
End Sub
But it gives me the error of:
但它给了我以下错误:
Constructor call is valid only as the first statement in an instance constructor.
Any help would be great to solve this issue!
任何帮助都会很好地解决这个问题!
update
更新
Private Sub SView_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim _sView As New SView
End Sub
It never hits the Load when I put a break on line Dim _sView As New SView
当我在Dim _sView As New SView线上休息时,它永远不会击中负载
回答by Thomas Levesque
How can I tell it to fire off the Public Sub New() function?
我怎样才能告诉它触发 Public Sub New() 函数?
You can't. Sub New()is the constructor of your class, which is executed to initialize an instance of the class. You can't call it after the object has been created.
你不能。Sub New()是类的构造函数,执行该构造函数以初始化类的实例。创建对象后不能调用它。
回答by field_b
Your default constructor (the one without any parameters) is called when the form is instantiated and prior to the Load event firing. If you want to verify this, place a breakpoint within the constructor and debug your solution.
您的默认构造函数(没有任何参数的构造函数)在窗体实例化时和 Load 事件触发之前被调用。如果您想验证这一点,请在构造函数中放置一个断点并调试您的解决方案。
回答by ??ssa P?ngj?rdenlarp
Sub Newis called when you create a New instance.
"Sub New" ===> "Dim foo As NewFooBar" Get it?
Sub New创建新实例时调用。
“Sub New” ===> “Dim foo As NewFooBar” 明白了吗?
Some confusion may be because your SView constructor is overloaded:
一些混淆可能是因为您的 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
It can be called with or without a reference to a controller:
可以在有或没有对控制器的引用的情况下调用它:
Dim _sView As New SView
' or
Dim _ctrlr As New SController
Dim _sView As New SView(_ctrlr)
In the first case, your SView will create its own new controller. In the second, it will use the one passed to it in the constructor call (New SView(_ctrlr)). In bothcases your new object willhave a controller object.
在第一种情况下,您的 SView 将创建自己的新控制器。在第二种情况下,它将使用在构造函数调用 ( New SView(_ctrlr)) 中传递给它的那个。在这两种情况下,您的新对象都将有一个控制器对象。
In cases where a new class object should not exist without the ctor arg, just remove the simple (empty) constructor so an exception is thrown:
在没有 ctor arg 的情况下不应该存在新的类对象的情况下,只需删除简单(空)构造函数,以便抛出异常:
Dim emp As New Employee(empName)
' this one fails if you remove the "Sub New()":
Dim emp As New Employee()
If you only ever want to create new instances with the controller you created, just remove the simple/empty constructor.
如果您只想使用您创建的控制器创建新实例,只需删除简单/空构造函数。
EDIT
编辑
It never hits the Load when I put a break on line [Dim _sView As New SView]
It never hits the Load when I put a break on line [Dim _sView As New SView]
Loading a form is not the same as creating a form instance. Forms are just classes and the Load event is called the first timeyou use invoke the Showmethod on the object. This is a goodthing as it allows us to create a form instance and work with it beforeit is shown. Longer notation:
加载表单与创建表单实例不同。表单只是类,当您第一次Show在对象上使用 invoke方法时会调用Load 事件。这是一个很好的事情,因为它使我们能够使用它创建一个表单实例,工作之前显示它。更长的符号:
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)
FormSetupmight be a method I write which is some tasks to be done only the first time it is used. If I subsequently just Hidethe form, I can skip all that rather than have it in a Loadevent. The comments however explain whats going on for each line.
FormSetup可能是我写的一种方法,它是一些只有在第一次使用时才能完成的任务。如果我随后只是Hide表单,我可以跳过所有这些,而不是在Load事件中使用它。然而,评论解释了每一行的情况。
回答by D Stanley
IF you want to call the logic in the constructor on an already-constructed instance, you need to refactor to move that logic out of the constructtheitroad:
如果您想在已构造的实例上调用构造函数中的逻辑,则需要重构以将该逻辑移出构造函数:
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
Then instead of calling Me.New(New SController())you'll call Me.Initialize(New SController())
然后Me.New(New SController())你会打电话而不是打电话Me.Initialize(New SController())

