处理对象时ByVal和BeRef之间的差异 [英] Differance between ByVal and BeRef when handling objects

查看:85
本文介绍了处理对象时ByVal和BeRef之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我学到的是变量只是处理对象时的参考。

您是否应该在函数中使用ByVal或ByRef?他们产生相同的结果或者我错过了什么?

问候

/ Niklas


Public Class Main

Shared Sub Main()

Dim testPropObj As New MyPropertObject

testPropObj.MyInt = 1

Console.WriteLine("g:org: testPropObj.MyInt ="& testPropObj.MyInt)

ChangeObjectByVal(testPropObj)

Console.WriteLine(" ByVal:testPropObj ="& testPropObj.MyInt)

ChangeObjectByRef(testPropObj)

Console.WriteLine(" ByRef:testPropObj ="& testPropObj.MyInt)

Console.WriteLine( 按Enter退出...。

Console.ReadLine()

结束子


公共共享子ChangeObjectByVal(ByVal myObject As MyPropertObject)

myObject.MyInt = 5

End Sub


Public Shared Sub ChangeObjectByRef(ByRef myObject As MyPropertObject)

myObject.MyInt = 6

End Sub

End课程


公共课MyPropertObject

公共MyInt为整数

结束课

Hi
What I have learned is that a variable is just a reference when dealing with Objects.
Are you supposed to use ByVal or ByRef in functions? They produce the same result or have I missed something?
Regards
/Niklas

Public Class Main
Shared Sub Main()
Dim testPropObj As New MyPropertObject
testPropObj.MyInt = 1
Console.WriteLine("Org: testPropObj.MyInt = " & testPropObj.MyInt)
ChangeObjectByVal(testPropObj)
Console.WriteLine("ByVal: testPropObj = " & testPropObj.MyInt)
ChangeObjectByRef(testPropObj)
Console.WriteLine("ByRef: testPropObj = " & testPropObj.MyInt)
Console.WriteLine("Press Enter to exit...")
Console.ReadLine()
End Sub

Public Shared Sub ChangeObjectByVal(ByVal myObject As MyPropertObject)
myObject.MyInt = 5
End Sub

Public Shared Sub ChangeObjectByRef(ByRef myObject As MyPropertObject)
myObject.MyInt = 6
End Sub
End Class

Public Class MyPropertObject
Public MyInt As Integer
End Class

推荐答案




你得到的输出确实是正确的。


方式ByVal& ByRef在引用类型的情况下不同如下:当一个

ref类型变量传递ByVal时,传递引用var的副本

(即两个vars用于同一个对象)。因此,如果您将被调用方法中的

类的新实例分配给ByVal var,则原始内容仍保持原样。另一方面,ByRef按原样传递引用var。调用方法中任何新的

引用赋值都将改变原始var

指向的内容。


我修改了下面的代码显示差异。运行它并检查

结果。


" Niklas" <镍**** @ discussions.microsoft.com>在留言中写道

news:18 ********************************** @ microsof t.com ...



我学到的是变量只是处理

对象时的参考。 />
你应该在函数中使用ByVal还是ByRef?他们产生相同的

结果或者我错过了什么?

问候

/ Niklas


公共类主要

共享子主()

Dim testPropObj作为新的MyPropertObject

testPropObj.MyInt = 1

Console.WriteLine(" org:testPropObj.MyInt ="& testPropObj.MyInt)

ChangeObjectByVal(testPropObj)

Console.WriteLine(" ByVal:testPropObj = "& testPropObj.MyInt)

ChangeObjectByRef(testPropObj)

Console.WriteLine(" ByRef:testPropObj ="& testPropObj.MyInt)

Console.WriteLine(按Enter退出...)

Console.ReadLine()

结束子

Public Shared Sub ChangeObjectByVal(ByVal myObject As MyPropertObject)

myObject = New MyPropertObject

myObject.MyInt = 5

End Sub


Public Shared Sub ChangeObjectByRef(ByRef myObject As MyPropertObject)

myObject = New MyPropertObject

myObject.MyInt = 6

End Sub

结束班级


公开MyPropertObject类

Public MyInt As Integer

End Class
Hi,

The output you are getting is indeed correct.

The way ByVal & ByRef differs in case of reference type is like this: When a
ref type variable is passed ByVal, a copy of the reference var is passed
(ie, two vars for the same object). So, if you assign a new instance of the
class inside the called method to the ByVal var, the original remains
intact. On the other hand, ByRef passes the reference var as is. Any new
reference assignment in the called method will change what the original var
was pointing at.

I have modified the code below to show the difference. Run it and check the
results.

"Niklas" <Ni****@discussions.microsoft.com> wrote in message
news:18**********************************@microsof t.com...
Hi
What I have learned is that a variable is just a reference when dealing with
Objects.
Are you supposed to use ByVal or ByRef in functions? They produce the same
result or have I missed something?
Regards
/Niklas

Public Class Main
Shared Sub Main()
Dim testPropObj As New MyPropertObject
testPropObj.MyInt = 1
Console.WriteLine("Org: testPropObj.MyInt = " & testPropObj.MyInt)
ChangeObjectByVal(testPropObj)
Console.WriteLine("ByVal: testPropObj = " & testPropObj.MyInt)
ChangeObjectByRef(testPropObj)
Console.WriteLine("ByRef: testPropObj = " & testPropObj.MyInt)
Console.WriteLine("Press Enter to exit...")
Console.ReadLine()
End Sub

Public Shared Sub ChangeObjectByVal(ByVal myObject As MyPropertObject)
myObject = New MyPropertObject
myObject.MyInt = 5
End Sub

Public Shared Sub ChangeObjectByRef(ByRef myObject As MyPropertObject)
myObject = New MyPropertObject
myObject.MyInt = 6
End Sub
End Class

Public Class MyPropertObject
Public MyInt As Integer
End Class


Hi Niklas,


你几乎可以永远使用ByVal

它的值是值本身,而现有对象的值是

对象的引用。 />

以上我写的时候不能使用ByVal,也就是说当对象

被声明但是还没有创建。比你必须为一个物体做这个

ByRef。


我希望这有帮助吗?


Cor
Hi Niklas,

You can almost forever use ByVal
It is with a value the value itself and with an existing object the value of
the reference of the object.

Above I write already when it cannot be used ByVal, that is when the object
is declared however not created yet. Than you have to do it for an object
ByRef.

I hope this helps?

Cor


谢谢。这意味着我必须重新设计我的应用程序,因为属性的Set部分不允许ByRef只有ByVal ...也许我使用一个字段。

问候

/ Niklas


" Shiva"写道:
Thank you. That means that I have to redesign my application because the Set part of Property do not allow ByRef only ByVal...maybe I use a field.
Regards
/Niklas

"Shiva" wrote:


你得到的输出确实是正确的。

ByVal& ByRef在引用类型的情况下不同如下:当一个
ref类型变量传递ByVal时,传递引用var的副本
(即,同一对象的两个vars)。因此,如果您将被调用方法中的
类的新实例分配给ByVal var,则原始内容保持不变。另一方面,ByRef按原样传递引用var。调用方法中的任何新的
引用赋值都将改变原始var指向的内容。

我修改了下面的代码来显示差异。运行它并检查
结果。

Niklas <镍**** @ discussions.microsoft.com>在消息中写道
新闻:18 ********************************** @ microsof t.com。 ..

我学到的是变量只是处理对象时的参考。
你应该在函数中使用ByVal还是ByRef?他们产生相同的结果或者我错过了什么?
问候
/ Niklas

公共班主要
共享子主()
Dim testPropObj As New MyPropertObject
testPropObj.MyInt = 1
Console.WriteLine(" org:testPropObj.MyInt ="& testPropObj.MyInt)
ChangeObjectByVal(testPropObj)
Console.WriteLine(" ByVal:testPropObj ="& testPropObj.MyInt)
ChangeObjectByRef(testPropObj)
Console.WriteLine(" ByRef:testPropObj ="& testPropObj.MyInt)
Console.WriteLine(按Enter键退出...)
Console.ReadLine()
End Sub
公共共享子ChangeObjectByVal(ByVal myObject As MyPropertObject )
myObject = New MyPropertObject
myObject.MyInt = 5
End Sub
公共共享Sub ChangeObjectByRef(ByRef myObject As MyPropertObject)
myObject = New MyPropertObject
myObject.MyInt = 6
End Sub
End Class

Public Cla ss MyPropertObject
Public MyInt As Integer
End Class
Hi,

The output you are getting is indeed correct.

The way ByVal & ByRef differs in case of reference type is like this: When a
ref type variable is passed ByVal, a copy of the reference var is passed
(ie, two vars for the same object). So, if you assign a new instance of the
class inside the called method to the ByVal var, the original remains
intact. On the other hand, ByRef passes the reference var as is. Any new
reference assignment in the called method will change what the original var
was pointing at.

I have modified the code below to show the difference. Run it and check the
results.

"Niklas" <Ni****@discussions.microsoft.com> wrote in message
news:18**********************************@microsof t.com...
Hi
What I have learned is that a variable is just a reference when dealing with
Objects.
Are you supposed to use ByVal or ByRef in functions? They produce the same
result or have I missed something?
Regards
/Niklas

Public Class Main
Shared Sub Main()
Dim testPropObj As New MyPropertObject
testPropObj.MyInt = 1
Console.WriteLine("Org: testPropObj.MyInt = " & testPropObj.MyInt)
ChangeObjectByVal(testPropObj)
Console.WriteLine("ByVal: testPropObj = " & testPropObj.MyInt)
ChangeObjectByRef(testPropObj)
Console.WriteLine("ByRef: testPropObj = " & testPropObj.MyInt)
Console.WriteLine("Press Enter to exit...")
Console.ReadLine()
End Sub

Public Shared Sub ChangeObjectByVal(ByVal myObject As MyPropertObject)
myObject = New MyPropertObject
myObject.MyInt = 5
End Sub

Public Shared Sub ChangeObjectByRef(ByRef myObject As MyPropertObject)
myObject = New MyPropertObject
myObject.MyInt = 6
End Sub
End Class

Public Class MyPropertObject
Public MyInt As Integer
End Class



这篇关于处理对象时ByVal和BeRef之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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