1

I am debugging a project right now and it has a function with the following signature:

Public Function changeRemoteDirectory(ByVal newDirectory As String, Optional ByVal Direction As Boolean = True) As Boolean

    MsgBox(direction)

    'rest of code

End Function

I was trying to figure out what was causing this function to return a value of False when I knew that it should return True given the input that I was supplying, so I put MsgBox(direction) into the Function to see what the value of direction was when I called the Function. I called the function like this, yet I received a MsgBox that showed the value of direction to be False:

changeRemoteDirectory("/internal")

The first parameter works just fine and the code that requires it to execute works correctly, but I cannot figure out why Direction has a value of False in a situation where, I believe, it should have its default value of True. I'm not totally opposed to rewriting the Function if need be, but can anybody here discern why Direction does not have a value of True when the function changeRemoteDirectory() is called without the second parameter supplied?

4

1 回答 1

2

听起来您正在经历可选参数导致的众多痛苦之一。

当调用的代码changeRemoteDirectory被编译时,可选参数被注入到调用中,就像const在编译时替换 a 一样。如果不重新编译调用者,对可选参数值的更改将不会生效。

有关更多信息,请参阅本文

通常,您应该使用方法重载而不是可选参数。您可以获得所有相同的功能,而没有痛苦和性能缺陷。

于 2013-07-12T13:06:01.327 回答