对于每个变量作为一个本地变量? [英] For each variable acting like a local var?

查看:167
本文介绍了对于每个变量作为一个本地变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶然发现了一些我不太明白的逻辑。让我们用下面的一段代码:

$ pre $ For Each ds As DerivedScale In List
If ds.ScaleID = scaleId Then
ds.ScaleID = ds.ScaleID + scaleStep
CType(List(myCounter + scaleStep),DerivedScale).ScaleID = scaleId
myDerivedScale = ds
ds = List(myCounter + scaleStep )< ---------------------
List(myCounter + scaleStep)= myDerivedScale
Exit
End If
myCounter + = 1
下一个

并更改序列号(scaleid)。箭头表示发生问题的位置。项目ds由更高/更低的对象1索引号代替。但是,这不会影响列表中的对象。所以当我检查,项目ds没有设置。然而,当我看着ds.ScaleId = ds.ScaleID + scaleStep时,这反映在List中。



所以我想知道的是:是ds在这里像一个局部变量,我可以只改变它的属性?

在此先感谢。

ds是对列表引用的对象的引用。所以当你设置属性的时候,这些改变也反映在列表中。但是因为ds只是引用,所以你猜测,改变引用的内容不会影响列表。


I stumbled upon something i don't quite see the logic of. Let's ork with following piece of code:

For Each ds As DerivedScale In List
    If ds.ScaleID = scaleId Then
        ds.ScaleID = ds.ScaleID + scaleStep
        CType(List(myCounter + scaleStep), DerivedScale).ScaleID = scaleId
        myDerivedScale = ds
        ds = List(myCounter + scaleStep)          <---------------------
        List(myCounter + scaleStep) = myDerivedScale
        Exit For
    End If
    myCounter += 1
Next

This piece is written for 2 records to change place and change the sequence number (scaleid). The arrow indicates where the issue occurs. The item "ds" is replaced by the object 1 indexnumber higher/lower. This does however not effect that object in the List. So when i check, item ds isn't set.

However, when i look at ds.ScaleId = ds.ScaleID + scaleStep, this is reflected in the List.

So what I am wondering is: is "ds" acting like a local variable here, and can i only make changes to it's properties?

Thanks in advance.

解决方案

ds is a reference to an object that is also referenced by the list. So when you set properties on it, those changes are reflected in the list as well. But since ds is just reference, as you surmise, changing what it refers to will not affect the list.

这篇关于对于每个变量作为一个本地变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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