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() CallingArg = "ABCDEF" CalledSub CallingArg MsgBox (CallingArg)End SubPublic Sub CalledSub(CalledArg) CalledArg = "987654"End Sub
Or the code could look like this for a Function:
Function code to return a value from a procedure call
Public Sub CallingSub() CallingArg = "ABCDEF" CalledFunc CallingArg MsgBox (CallingArg)End SubPublic Function CalledFunc(CalledArg) CalledArg = "987654" CalledFunc = "Any Return Value"End Function
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.
No variables are declared.This means that all of the variables are of type "Variant" - similar to "Object" in VB.NET.
No scoping or typing is done in the argument list.This means that everything uses the default settings.
The procedure call doesn't use any parentheses or a Call keyword.
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.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment