你好,这个网站是我的nr1代码灵感,现在我有一个问题,我自己,我无法修复。
我有一个模块CommCtrl,它在另一个类中包含一个Tagcollection --我对这个集合进行了引用--如果我用项填充引用,那么条目只添加在这个类中,而不是在CommCtrl类中。我不明白为什么推荐信不起作用。
Public Module CommCtrl
Public TagCollection As New List(Of Tag)
Private WayPointManager As WayPointClass
Public Sub BuildConfigData()
WayPointManager = New WayPointClass()
WayPointManager.SetTagListReference(TagCollection)
' Count items in Tagcollection here is 0 items
call WayPointManager.FillTags
' Count items in Tagcollection here is still 0 items??
End Sub
End module
Public Class WayPointClass
Private TagListReference As List(Of Tag)
Public Sub SetTagListReference(ByRef TagList As List(Of Tag))
TagListReference = TagList
End Sub
Public Function FillTags() As Boolean
TagListReference = XmlBuddy.Deserialize(reader) ' Fill Up the taglist
' Count items in TagListReference here is 100 items
End Function
End Class发布于 2014-06-23 08:38:57
方法的参数with ByRef关键字只能在赋值的左侧出现该参数时才能发生。
sub modify(byref p as integer)
p = 1 ' byref param on the LHS of an assignment
end sub
dim a as int = 0
call modify(a)
console.writeline(a) ' prints 1发布于 2014-06-23 09:21:34
ByRef工作得很好。正在发生的情况是:
当您第一次调用WayPointClass.SetTagListReference方法时,您将TagListReference字段设置为指向传入的List(of Tag)参数(在您的示例中,这将是CommCtrl.TagCollection)。
但是,当您调用WayPointClass.SetTags时,您并没有像您想象的那样设置原始列表(CommCtrl.TagCollection)的值,您实际上是在将TagListReference更改为指向一个新列表,并且没有指向原始CommCtrl.TagCollection的链接。
换句话说,最初您是将TagListReference (A)指向TagCollection ( B ),即A -> B,但之后是A -> C(XmlBuddy.Deserialize(reader)的结果),而B是不变的。
正如Hans所说,您可以通过将CommCtrl.TagCollection传递到“`FillTags”方法来避免这种情况,因此您肯定要修改该列表。
https://stackoverflow.com/questions/24361533
复制相似问题