将结构传递给功能-更改原始结构 [英] Passing Structure to Function - Changes Original Structure

查看:69
本文介绍了将结构传递给功能-更改原始结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,全部:

我是VB.NET领域的新手,正在尝试将结构数组传递给函数,并将其传递给具有更新值的新结构数组.下面代码中的所有内容似乎都可以正常工作,返回的结构数组是完美的,但它也改变了原来的结构.我在做什么错?!

Hello, all:

I''m new to the VB.NET world and am trying to pass a structure array to a function and have it pass me back a new structure array with update values. Everything in the code below seems to work fine, the returned structure array is perfect, but it also changes the original. What am I doing wrong?!

Public Class Form1
    Structure SpaceCurve
        Dim x As Double
        Dim y As Double
        Dim SpaceName As String
    End Structure
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim n As Integer
        'ReDim OrigSC(10)
        Dim OrigSC(10) As SpaceCurve
        Dim WorkSC(10) As SpaceCurve
        For n = 0 To 10
            OrigSC(n).x = n * 1.5
            OrigSC(n).y = n * 2.5
            OrigSC(n).SpaceName = "Space" & CStr(n)
            Console.WriteLine(OrigSC(n).x & "," & OrigSC(n).y & "," & OrigSC(n).SpaceName)
        Next
        WorkSC = UpdateSC(OrigSC, "-test")
        For n = 0 To 10
            Console.WriteLine(WorkSC(n).x & "," & WorkSC(n).y & "," & WorkSC(n).SpaceName)
        Next
        For n = 0 To 10
            Console.WriteLine(OrigSC(n).x & "," & OrigSC(n).y & "," & OrigSC(n).SpaceName)
        Next

    End Sub
    Function UpdateSC(ByVal SCStruct() As SpaceCurve, ByVal SpaceAppend As String) As SpaceCurve()
        Dim x As Integer
        For x = 0 To UBound(SCStruct)
            SCStruct(x).x = 1.0
            SCStruct(x).y = 3.3
            SCStruct(x).SpaceName = SCStruct(x).SpaceName & SpaceAppend
        Next
        UpdateSC = SCStruct
    End Function

End Class



控制台...



Console...

0,0,Space0
1.5,2.5,Space1
3,5,Space2
4.5,7.5,Space3
6,10,Space4
7.5,12.5,Space5
9,15,Space6
10.5,17.5,Space7
12,20,Space8
13.5,22.5,Space9
15,25,Space10
1,3.3,Space0-test
1,3.3,Space1-test
1,3.3,Space2-test
1,3.3,Space3-test
1,3.3,Space4-test
1,3.3,Space5-test
1,3.3,Space6-test
1,3.3,Space7-test
1,3.3,Space8-test
1,3.3,Space9-test
1,3.3,Space10-test
1,3.3,Space0-test
1,3.3,Space1-test
1,3.3,Space2-test
1,3.3,Space3-test
1,3.3,Space4-test
1,3.3,Space5-test
1,3.3,Space6-test
1,3.3,Space7-test
1,3.3,Space8-test
1,3.3,Space9-test
1,3.3,Space10-test

推荐答案

请参阅
See http://msdn.microsoft.com/en-us/library/aa903253(v=vs.71).aspx[^]

What happens when you pass an object (or any reference type) ''ByVal''?

Sub xxx

 Dim A as MyObj = new MyObj
 MySub(A)

End Sub


Sub MySub (ByVal Param As MyObj)

 Param.Member = NewVal

End Sub




在MySub调用之后,A仍指向同一个对象-MySub即使希望也无法更改其指向的对象(如果传递了ByRef,则可以重新分配参数,因此使A指向完全不同的MyObj对象). />
但这并不意味着MySub无法更改该对象的成员的值-因此,上面的代码将更改A.Member(A.Member发生了变化-但A没有,它仍然是同一对象). >
如果您想要一个具有不同成员值的对象而不更改A,则需要在MySub中创建一个新对象以首先复制成员值.




After the MySub call A still points to the same object - and MySub cannot change which object A points to even if it wanted to (if passed ByRef, it could reassign param and so make A point to a completely different MyObj object).

But that does NOT mean that MySub cannot change the values of members of that object - so the code above will change A.Member (A.Member changes - but A doesn''t, it is still the same object).

If you want an object with different member values without changing A you need to create a new object within MySub to duplicate the member values first.


ByVal并不代表您的想法! br/>
MSDN [

在您的示例中,UpdateSC 不能更改OrigSC的值,但是可以并且确实可以更改OrigSC
中包含的元素的值.
就像以新用户身份登录Windows:您无法更改旧用户的桌面设置,但是您都使用相同的键盘!
ByVal does not mean what you think!

MSDN[^]

"If you pass a variable argument by value using the ByVal keyword, the procedure cannot modify the variable itself. However, if the argument is a reference type, you can modify the members of the object to which it points, even though you cannot replace the object itself. In particular, you can modify the members of the object. For example, if the argument is an array variable, you cannot assign a new array to it, but you can change one or more of its elements. The changed elements are reflected in the underlying array variable in the calling code."

In your example, UpdateSC cannot change the value of OrigSC, but it can, and does change the values of elements contained within OrigSC

It''s like logging in to Windows as a new user: You can''t change your old users desktop settings, but you both use the same keyboard!


您的结构是一个值类型,但您的数组是引用类型.当按值传递数组时,无论如何传递其引用,而不传递数组的副本.即使结构是值类型,当您将其修改为数组元素时,也会在数组中替换它.调用方的变量是数组,而不是结构,因此修改了此变量(数组)引用的对象.

—SA
Your structure is a value type, but your array is a reference type. When you pass the array by value, you pass its reference anyway, never the copy of the array. Even though the structure is a value type, it is replaced in the array when you modify it as an array element. Your caller''s variable is array, not structure, so the object referenced by this variable (array) is modified.

—SA


这篇关于将结构传递给功能-更改原始结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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