即使使用ByVal,也可以更改调用参数.在Visual Studio中出错? [英] Calling parameter can be changed despite using ByVal. Bug in Visual Studio?

查看:101
本文介绍了即使使用ByVal,也可以更改调用参数.在Visual Studio中出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

程序中发生了一件奇怪的事情.当我将参数传递给过程ByVal并在过程中对其进行更改时,原始(调用)参数也将更改!我是在忽略简单的东西吗,是否与程序的结构有关,还是偶然发现了Visual Studio(2008)中的错误?

我的(Windows服务)程序的结构如下:

There is a strange thing going on in my program. When I pass a parameter to a procedure ByVal, and change it within the procedure, the original (calling) parameter is changed too! Am I overlooking something simple, does it have to do with the structure of my program or have I stumbled on a bug in Visual Studio (2008)?

The structure of my (Windows Service) program is as follows:

Private Sub JobTimer_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles JobTimer.Elapsed

    Dim lPortfolio As New Portfolio
    Dim lPortfolios As New Portfolios

    lPortfolios = lPortfolio.PerformHistoricalBacktest()

End Sub




在下面可以找到我的投资组合类.它包含共同基金.




Underneath you can find my Portfolio class. It contains mutual funds.

Public Class Portfolio

    Private _funds As List(Of Fund)

    Public Sub New()
          _funds = New List(Of Fund)
    End Sub


    Public Overloads Function PerformHistoricalBacktest()

        Dim j As Integer
        Dim lNrOfPeriods As Integer = 8
        Dim lPortfolio As Portfolio
        Dim lPortfolios As New Portfolios


        For j = 1 To lNrOfPeriods

            Me.CalculateStatisticForEachFund()

            lPortfolio = New Portfolio
            lPortfolio = Me.GetFunds()  'Gets / returns part of the funds.
            lPortfolios.Add(portfolio:=lPortfolio)
            lPortfolio = Nothing

        Next j

        Return lPortfolios

    End Function


    Public Function GetFunds() As Portfolio

        Dim lPortfolioQualifying As Portfolio

        lPortfolioQualifying = New Portfolio

        For Each lFund As Fund In Me._funds

            With lFund

                If <checks if the particular lFund should be returned> Then

                    lPortfolioQualifying.AddFund(lFund)

                End If

            End With

        Next lFund

        Return lPortfolioQualifying

    End Function


    Public Sub AddFund(ByVal fundToAdd As Fund)

        'If I change 'fundToAdd' here, the calling parameter is changed too!!

        _funds.Add(item:=fundToAdd)

    End Sub

End Class




在下面可以找到我的投资组合类.它包含在不同时间创建的投资组合.




Underneath you can find my Portfolios class. It contains portfolios created at different moments in time.

Public Class Portfolios


    Private _portfolios As List(Of Portfolio)


    Public Sub New()

        _portfolios = New List(Of Portfolio)

    End Sub


    Public Sub Add(ByVal portfolio As Portfolio)

        'If I change 'portfolio' here, the calling parameter is changed too!!

        _portfolios.Add(portfolio)

    End Sub

End Class

推荐答案

您正在传递对象引用而不是值类型.

问候
Espen Harlinn
You are passing object references not value types.

Regards
Espen Harlinn


答案1是正确的

如果传递对对象的引用,则传递ByVal时不能更改的是引用,而不是内容.

如果您分配或重新分配对象,则原始引用将保持不变
一旦通话返回.
Answer 1 is correct

If you pass a reference to an object, it is the reference that cannot change when passed ByVal, not the contents.

If you assign or re-assign the object, the original reference will persist
once the call returns.


好吧,听起来您需要使Portfolio或Fund不可变.
将所有属性设置为ReadOnly,然后通过构造函数传递值.
为方便起见,添加Clone()函数或复制构造函数.

这将停止对单个属性的更新,并使所有属性保持一致.


Well, sounds like you need to make Portfolio or Fund immutable.
Make all the properties ReadOnly and pass in the values via a Constructor.
As an added convience, add a Clone() Function or Copy Constructor.

This will stop updates to individual properties and keep everything consistent.


Public Class Fund
    Public ReadOnly Name As String
    Public ReadOnly Amount As Decimal

    Public Sub New(ByVal name As String, ByVal amount As Decimal)
        Me.Name = name
        Me.Amount = amount
    End Sub

    Public Sub New(ByVal copyOf As Fund)
        Me.Name = copyOf.Name
        Me.Amount = copyOf.Amount
    End Sub
End Class



这将创建一个不错的线程安全类.

但是,如果确实需要读/写属性,则使它们成为必需,但仍将副本传递给AddFund.说:

AddFund(新基金(基金))

我不确定为什么您也不能在函数中使用临时变量
而不是更新对象,但是没有代码的余地,很难看到发生了什么.



This creates a nice thread safe class.

However, if you do need read/write properties, then make them so but still pass in a copy to AddFund. Say:

AddFund(New Fund(fund))

I am not sure why you just cannot use temp variables in the function either
instead of updating the object, but without the reat of the code its difficult to see whats going on.


这篇关于即使使用ByVal,也可以更改调用参数.在Visual Studio中出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆