如何将变量的值更改为另一个子项?像这样的东西。
Sub Main()
Dim valor as Integer
valor = 0
Call Increment
MsgBox valor 'expeted 30
End Main
Sub Increment()
valor = 30
End Main
答案 0 :(得分:3)
另一种选择-通过函数返回更改。
Sub Main()
Dim valor as Integer
valor = 0
' whatever code here
valor = Increment(valor)
MsgBox valor 'expected 30
End Main
Function Increment(ValueToBeIncremented as Integer) As Integer
ValueToBeIncremented = ValueToBeIncremented + 30
End Main
您原来的“增量”例程实际上并未增量,而是设置了一个值-因此我在此处进行了修改。
答案 1 :(得分:0)
使用公共变量仅限于您不能依赖通过子变量或函数传递变量的情况
在这种情况下,可以这样,增强您的“ changer”子句以接受参数,并使主子句传递一个参数:
Sub Main()
Dim valor as Integer
valor = 0
Increment valor ' pass valor to Increment() sub (no need far "Call" keyword)
MsgBox valor 'expected 30
End Main
Sub Increment(valor as Integer) ' have Increment accept an integer argument
valor = 30 ' change the value of the passed argument
End Main
参数类型必须与传递的变量一(在本例中为Integer)相同,而名称则不得如此,因此以下内容有效:
Sub Increment(myValor as Integer) ' have Increment accept an integer argument
myValor = 30 ' change the value of the passed argument
End Main
答案 2 :(得分:-2)
express-cassandra