[英]ByRef not carrying Through IN vb.net
The one calls form 2 as a dialog, and passed by ref a "pointer" to the base class(abstract). 一个调用形式2作为对话框,并由ref传递“指针”给基类(抽象)。 //Form 1 calling form two.
// Form 1调用Form 2。 And Passing a ref object
并传递一个ref对象
Dim CreateForm As New frmCreate(Robot)
//Second Forms Overloaded New //第二个表格重载了
Public Sub New(ByRef Robot As cRobot)
InitializeComponent()
thisRobot = Robot
End Sub
Select Case (cbType.SelectedIndex)
Case 0
lblOther.Text = "Bullet Proof Value"
Dim SecRobot = New cSecurityRobot
SecRobot.Name = txtName.Text
SecRobot.Temperature = nudTemp.Value
SecRobot.Threshold = nudThreshold.Value
SecRobot.BulletproofValue = nudOther.Value
thisRobot = SecRobot
Case 1
lblOther.Text = "Special Moves"
Dim SpRobot = New cSportsRobot
SpRobot.Name = txtName.Text
SpRobot.Temperature = nudTemp.Value
SpRobot.Threshold = nudThreshold.Value
SpRobot.SpecialMoves = nudOther.Value
thisRobot = SpRobot
Case 2
lblOther.Text = "Domestic Skills"
Dim SerRobot = New cServiceRobot
lblOther.Text = "Domestic Skills"
SerRobot.Name = txtName.Text
SerRobot.Temperature = nudTemp.Value
SerRobot.Threshold = nudThreshold.Value
SerRobot.DomesticSkills = nudOther.Value
thisRobot = SerRobot
Case Else
lblOther.Text = "Bullet Proof Value"
Dim SecRobot = New cSecurityRobot
SecRobot.Name = txtName.Text
SecRobot.Temperature = nudTemp.Value
SecRobot.Threshold = nudThreshold.Value
SecRobot.BulletproofValue = nudOther.Value
thisRobot = SecRobot
End Select
Form 2 assigns some values and terminates, but there is always a NULL Exception occurring 表格2分配了一些值并终止,但是总是会发生NULL异常
Let's take a look at your constructor: 让我们看一下您的构造函数:
Public Sub New(ByRef Robot As cRobot)
InitializeComponent()
thisRobot = Robot '<-- Problem is here
End Sub
On the line indicated above, you are making a copy of the reference, and so the ByRef no longer helps you. 在上面指示的行上,您正在复制参考,因此ByRef不再帮助您。
Thinking about how to get around this problem, you might be able to do it by nesting the Robot inside another class: 考虑如何解决此问题,您可以通过将Robot嵌套在另一个类中来做到这一点:
Public Class RobotContainer
Public Property Robot As Robot
End Class
Pass a RobotContainer instance to your constructor in the normal (ByVal) way and keep a reference to that entire object in your class. 以常规(ByVal)方式将RobotContainer实例传递给构造函数,并在类中保留对整个对象的引用。 Now, both your frmCreate type and the calling code have a reference to the same object .
现在,您的frmCreate类型和调用代码都具有对同一object的引用。 When you update the Robot property on that object, it will be updated for both locations.
当您更新该对象的机械手属性时,将同时更新两个位置的属性。
But really, this whole design doesn't smell right. 但是,实际上,整个设计没有正确的气味。 Normally I would suggest a method that return the created robot, rather than trying to assign it to an outside location directly, but I understand that working with Windows Forms controls this may not be option.
通常,我建议使用一种返回创建的机械手的方法,而不是尝试直接将其分配给外部位置,但是我知道使用Windows Forms控件可能不是一种选择。 To suggest a better solution we'd need to see a lot more of your code.
为了提出更好的解决方案,我们需要查看更多代码。
Hmm... looking back at this I wanted to do something to make the RobotContainer more useful: 嗯...回头看一下,我想做些什么来使RobotContainer更有用:
Public Class ReferenceContainer(Of T)
Public Property Item As T
End Class
No, the "ByRef"-ness is only relevant for the method in which the parameter is declared. 不,“ ByRef” -ness 仅与声明参数的方法有关。 The value of the
thisRobot
variable is still just a reference value . thisRobot
变量的值仍然只是参考值 。 Changing the value of that variable later on won't change the caller's variable. 稍后更改该变量的值不会更改调用方的变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.