扫盲:byval和byref
你知道传递参数byval和byref的区别吗?byval的就一定不变吗?如果你不确定,请看代码,猜猜output是什么?
imports Microsoft.VisualBasic
imports System
imports System.Collections
public class TestClass
public a as integer
end class
public module MyModule
sub Main
dim a as integer
a = 5
TestValByVal(a)
dim b as new TestClass
b.a = 5
TestRefByVal(b)
dim c as new TestClass
c.a = 5
TestRefByVal(c)
WL(a)
wl(b.a)
wl(c.a)
RL()
end sub
public sub TestValByVal(byval a as integer)
a = 10
end sub
public sub TestRefByVal(byval b as TestClass)
b.a = 10
b= new TestClass
b.a = 15
end sub
public sub TestRefByRef(byref b as TestClass)
b.a = 10
b= new TestClass
b.a = 15
end sub
Helper methods
end module
imports System
imports System.Collections
public class TestClass
public a as integer
end class
public module MyModule
sub Main
dim a as integer
a = 5
TestValByVal(a)
dim b as new TestClass
b.a = 5
TestRefByVal(b)
dim c as new TestClass
c.a = 5
TestRefByVal(c)
WL(a)
wl(b.a)
wl(c.a)
RL()
end sub
public sub TestValByVal(byval a as integer)
a = 10
end sub
public sub TestRefByVal(byval b as TestClass)
b.a = 10
b= new TestClass
b.a = 15
end sub
public sub TestRefByRef(byref b as TestClass)
b.a = 10
b= new TestClass
b.a = 15
end sub
Helper methods
end module