Difference between ByVal and ByRef?In Visual Basic.Net, you can pass an argument to a function by value or by reference . This is known as the passing mechanism, and it determines whether the function can modify the programming element underlying the argument in the calling code. The function declaration determines the passing mechanism for each parameter by specifying the ByVal or ByRef keyword.
ByValByVal means that a copy of copy of the provided value will be sent to the function. For value types (Integer, Single, etc.) this will provide a shallow copy of the value. With larger types this can be inefficient. For reference types though (String, class instances) a copy of the reference is passed. Because a copy is passed in mutations to the parameter via = it won't be visible to the calling function . If you pass value, it's the same as if another variable is created at the method, so even if you modify it, the original variable (at the call site) won't have its value changed.
ByRefByRef in means that a reference to the original value will be sent to the function. It's almost like the original value is being directly used within the function. Operations like = will affect the original value and be immediately visible in the calling function . If you pass in a reference, when you modify the value in the method, the variable in the call site will also be modified. The default in Visual Basic is to pass arguments by value . You can make your code easier to read by using the ByVal keyword. It is good programming practice to include either the ByVal or ByRef keyword with every declared parameter.