Hi there!
For the last few days I've been running my code through the code analyzer that comes with Visual Studio 2005. I've found a whole bunch of useful design recommendations produced by this tool but have been stumped by one of them.
One of the warning messages recommends that I consider designing my subs and functions so that they do not accept ByRef arguments.
One of my subs is meant to fill a DropDownList with values for the web page.
Since DropDownLists with the same options are found in more than one location I've created a Sub that:
-> accepts a DropDownList object,
-> clears the list,
-> and fills the list with values
-> If the DropDownList supplied does not exist it create a new DropDownList and fill that one.
Another of my functions returns true or false depending on if it executed correctly and also returns (ByRef) an object that has had 6 of its properties modified by the function.
From what I understood I need to use the ByRef means of passing the argument in order to correctly pass back the information that I've modified.
However, when I change the ByRef to ByVal, they still work properly.
Looking up the difference between ByRef and ByVal in the help file I've found that:
By Ref:
-> If the procedure has a genuine need to change the underlying element in the calling code, declare the corresponding parameter ByRef.
-> If the correct execution of the code depends on the procedure changing the underlying element in the calling code, declare the parameter ByRef. If you pass it by value, or if the calling code overrides the ByRef passing mechanism by enclosing the argument in parentheses, the procedure call might produce unexpected results.
ByVal:
-> If the underlying element is modifiable, but you do not want the procedure to be able to change its value, declare the parameter ByVal. Only the calling code can change the value of a modifiable element passed by value.
So, according to the help, an argument passed ByVal should not result in the calling code's variable being modified by the sub/function.
This Does Not appear to be the case.
I am Very confused.
Could someone please clarify?
Thanks a lot
-Frinny
For the last few days I've been running my code through the code analyzer that comes with Visual Studio 2005. I've found a whole bunch of useful design recommendations produced by this tool but have been stumped by one of them.
One of the warning messages recommends that I consider designing my subs and functions so that they do not accept ByRef arguments.
One of my subs is meant to fill a DropDownList with values for the web page.
Since DropDownLists with the same options are found in more than one location I've created a Sub that:
-> accepts a DropDownList object,
-> clears the list,
-> and fills the list with values
-> If the DropDownList supplied does not exist it create a new DropDownList and fill that one.
Another of my functions returns true or false depending on if it executed correctly and also returns (ByRef) an object that has had 6 of its properties modified by the function.
From what I understood I need to use the ByRef means of passing the argument in order to correctly pass back the information that I've modified.
However, when I change the ByRef to ByVal, they still work properly.
Looking up the difference between ByRef and ByVal in the help file I've found that:
By Ref:
-> If the procedure has a genuine need to change the underlying element in the calling code, declare the corresponding parameter ByRef.
-> If the correct execution of the code depends on the procedure changing the underlying element in the calling code, declare the parameter ByRef. If you pass it by value, or if the calling code overrides the ByRef passing mechanism by enclosing the argument in parentheses, the procedure call might produce unexpected results.
ByVal:
-> If the underlying element is modifiable, but you do not want the procedure to be able to change its value, declare the parameter ByVal. Only the calling code can change the value of a modifiable element passed by value.
So, according to the help, an argument passed ByVal should not result in the calling code's variable being modified by the sub/function.
This Does Not appear to be the case.
I am Very confused.
Could someone please clarify?
Thanks a lot
-Frinny
Comment