ByRef参数不能按预期工作

时间:2013-07-28 17:47:16

标签: vb.net vb.net-2010

有人见过这个吗?

Public Shared Function IsAvailableByCampaignId(ByVal cn As SqlConnection, ByVal tr As SqlTransaction, ByVal campaignId As Integer, ByRef dest As PricingThresholds) As Boolean
    Dim retObj = ItemTypes.PricingThresholds.GetThresholds(cn, tr, campaignId)
    If retObj IsNot Nothing Then
        dest = New PricingThresholds(retObj)
    End If
    Dim retVal As Boolean = retObj IsNot Nothing
    Return retVal
End Function

当我调用内部

Dim retObj = ItemTypes.PricingThresholds.GetThresholds(cn, tr, campaignId)

我得到一个非null或没有retObj,但后来我用它来构建一个新的PricingThresholds,它是我需要返回的正确类型,并且我成功创建了一个有效的返回类型对象但是我回来了从外部调用parm传递的ByRef没有值,没有任何东西或null。

就像VB不工作一样。

我想我可以用不同的方式返回它。

1 个答案:

答案 0 :(得分:0)

下面的代码显示所有内容都按预期工作,即dest正在重新分配,并将其值保持在IsAvailableByCampaignId函数之外:

Sub Main()
  Dim dest As New PricingThresholds(1)
  Dim p = IsAvailableByCampaignId(dest)
End Sub

Class PricingThresholds
  Dim _id As Integer
  Sub New(id As Integer)
    _id = id
  End Sub
End Class

Public Function IsAvailableByCampaignId(ByRef dest As PricingThresholds) As Boolean
  dest = New PricingThresholds(2)
  Return True
End Function

随意使用此代码,看看您是否可以重现您所拥有的bevahior。