| You are here: | About>Computing & Technology>Visual Basic> Learn VB.NET> ByVal and ByRef: Passing Arguments in Visual Basic |
![]() | Visual Basic |
ByVal and ByRef: Passing Arguments in Visual BasicThis is another thing that changed in VB.NET.There are two ways to pass arguments to a procedure in Visual Basic: ByVal and ByRef. This refers to whether a copy of the argument is passed to the procedure (ByVal - the value is passed) or whether the actual argument itself is passed (ByRef - a reference to the argument is passed). The default for VB 6 is to call ByRef. There are also two types of procedures in Visual Basic: Function and Sub. The difference is that a Function returns the a value assigned to the function name and a Sub doesn't. So if you want to return a value to the calling routine, there are two ways to do it. In VB 6, the code that returns a value could look like this for a Sub: Sub code to return a value from a procedure call Public Sub CallingSub() Or the code could look like this for a Function: Function code to return a value from a procedure call Public Sub CallingSub() The result of both is a messagebox that displays the value "987654" that is returned as a result of changing the value of the argument. Note that the main difference is that the function has just one more statement to assign a return value to the function name. In VB 6, the difference was sometimes considered meaningless and programmers often used subroutines rather than functions out of habit or just to save a statement. A few other "shortcuts" in VB 6 should be pointed out.
This has some additional consequences. Let's look at those next. The illustration below shows an interesting result of simply changing whether parentheses are used or not in VB 6. -------- The explanation can be found in this note found buried pretty deeply in the VB 6 documentation: You are not required to use the Call keyword when calling a procedure. However, if you use the Call keyword to call a procedure that requires arguments, argumentlist must be enclosed in parentheses. If you omit the Call keyword, you also must omit the parentheses around argumentlist. If you use either Call syntax to call any intrinsic or user-defined function, the function's return value is discarded. Emphasis added. The actual behavior of the code doesn't seem to quite follow the documentation. But one thing is clear, the result is discarded. Not knowing this interesting bit of non-obvious behavior could result in a hard to find bug in VB 6. Because VB 6 didn't follow the conventions used by other languages and was generally confusing and inconsistent, Microsoft decided to scrap most of the procedure call syntax in VB 6 when they moved to VB.NET. In VB.NET, there is one way to do things and if you don't do it that way, an error results. Let's see what has to be done to convert this code to VB.NET. One way to convert the VB 6 project is to use the Upgrade Wizard. Just open the VB 6 project in VB.NET and the wizard takes over. But the Upgrade Wizard often misses a lot of optimization and can't convert some things at all. So a lot of programmers just copy and paste the code from VB 6 to VB.NET and correct the errors that result. If you do this with the first code example at the top of the article, there's just one error to correct: the fact that the variable CallingArg isn't declared. When this is declared as String, the program runs! But it doesn't quite get the right result as shown below: -------- The reason for the error this time is that when the VB 6 code was pasted into VB.NET, Intellisense corrected another "error" and added the VB.NET default, ByVal. This doesn't allow the actual value of the argument to be changed, only the copy made for the subroutine. If this is changed to ByRef in the VB.NET program, all is well again. This is one thing the Upgrade Wizard does right, by the way. It's changed to ByRef automatically when the Wizard is run. |
Las Vegas on a BudgetFind a BargainHotel DealsCheap EatsFree AttractionsEntertainment for Less |
All Topics | Email Article | | | ![]() |
| Advertising Info | News & Events | Work at About | SiteMap | Reprints | Help | Our Story | Be a Guide |
| User Agreement | Ethics Policy | Patent Info. | Privacy Policy | ©2008 About, Inc., A part of The New York Times Company. All rights reserved. |


