【问题标题】:ByRef argument does not work as expectedByRef 参数不能按预期工作
【发布时间】:2013-07-28 17:47:15
【问题描述】:

有人见过吗?

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 dest 传递的ByRef 没有任何值,并且是空值或空值。

这就像 VB 不工作。

我想我可以换一种方式退货。

【问题讨论】:

  • VB.NET 工作正常,您的代码没有做正确的事情。你能告诉我们你在哪里以及如何调用这个函数吗?问题可能就在那里。

标签: vb.net vb.net-2010


【解决方案1】:

下面的代码显示一切都按预期工作,即 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

随意使用此代码,看看您是否可以重现您的行为。

【讨论】:

    猜你喜欢
    • 2022-01-26
    • 1970-01-01
    • 2016-09-09
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    • 2013-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多