2013-07-28 24 views
0

任何人都看到過嗎?ByRef參數不能按預期方式工作

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)

我得到一個非空或沒有retObj,後來我用它來建立一個新的PricingThresholds它是正確的類型,我需要返回,併成功創建一個有效的返回類型的對象,但我從外部調用返回parm dest通過ByRef沒有價值,是什麼也沒有或null。

這就像VB不工作。

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

+0

VB.NET正在工作,你的代碼沒有做正確的事情。你能否告訴我們你在哪裏以及如何調用這個功能?問題可能在那裏。 – Styxxy

回答

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。