Passing an interface ByVal versus ByRef


Whether you pass an interface variable ByVal or ByRef has significance when the method call has to be made across contexts, for example, to an out-of-process component, or to a component on a different thread. When the call is made across contexts, parameters described as ByVal only get marshalled from the client to the server. Parameters described as ByRef get marshalled in both directions, that is, from the client to the server, and from the server to the client.
When you do not need your method to change the value of the passed in variable and pass it back to the client, you will get better performance by using ByVal. However, if you do not anticipate your method being used across contexts, you can use ByRef without any performance sacrifices.
When you implement an interface, IDL parameter attributes are used to decide whether the parameter will be ByRef or ByVal in Visual Basic.
  • Parameters with the [in] attribute are implemented as ByVal parameters.
  • Parameters with the [in,out] attribute are implemented as ByRef.
  • Parameters with the [out,retval] attributes are implemented as return values of the method.
  • Parameters with the [out] attribute are implemented as ByRef.