首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VB ByRef不工作

VB ByRef不工作
EN

Stack Overflow用户
提问于 2014-06-23 08:32:30
回答 2查看 438关注 0票数 1

你好,这个网站是我的nr1代码灵感,现在我有一个问题,我自己,我无法修复。

我有一个模块CommCtrl,它在另一个类中包含一个Tagcollection --我对这个集合进行了引用--如果我用项填充引用,那么条目只添加在这个类中,而不是在CommCtrl类中。我不明白为什么推荐信不起作用。

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-06-23 08:38:57

方法的参数with ByRef关键字只能在赋值的左侧出现该参数时才能发生。

代码语言:javascript
复制
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
票数 1
EN

Stack Overflow用户

发布于 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”方法来避免这种情况,因此您肯定要修改该列表。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24361533

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档