The terms parameter and argument are often used interchangeably, although they have entirely different meanings. Let us illustrate with an example. Consider the following function, which replicates a string a given number of times:
The variables sInput and iCount are the parameters of this function. Note that each parameter has an associated data type.
Now, when we call this function, we must replace the parameters by variables, constants, or literals,as in:
The items that we use in place of the parameters are called arguments.
Arguments can be passed to a function in one of two ways: by value or by reference. Incidentally, argument passing is often called parameter passing, although it is the arguments and not the parameters that are being passed.
The declaration of RepeatString given earlier contains the keyword ByVal in front of each parameter. This specifies that arguments are passed by value to this function. Passing by value means that the actual value of the argument is passed to the function. This is relevant when an argument is a variable. For instance, consider the following code:
The final line:
actually displays the number 20. In other words, the line:
does nothing. The reason is that the argument iAge is passed to the procedure Inc by value. Since only the value (in this case 20) is passed, that value is assigned to a local variable named x within the procedure. This local variable is increased to 21, but once the procedure ends, the local variable is destroyed. The variable iAge is not passed to the procedure, so its value is not changed.
On the other hand, if we modify the definition of the procedure Inc, replacing ByVal with ByRef, the story is different:
In this case, what is passed to the procedure Inc is a reference to the argument iAge. Hence, the procedure actually operates on the variable passed to it, incrementing the value of iAge to 21. Put another way, the variable represented by the parameter x is actually the passed variable iAge. In VB .NET, the default method of argument passing for arguments is by value. This is a change from earlier versions of VB, in which the default method was by reference.
There is a subtlety in argument passing with parameters of any object type. Actually, the subtlety occurs because an object variable is a pointer ; that is, it contains a reference to (or the address of) the object.
If we pass an object variable by value, we are passing the contents of the variable, which is the address of the object. Thus, any changes made in the called procedure affects the object itself, not a copy of the object. This seems like passing by reference, but it is not. Think of it this way: passing the value of an object's address is passing a reference to the object.
On the other hand, if we pass an object variable by reference, we are passing the address of the variable. In other words, we are passing the address of the address of the object! In languages that support pointers, this is referred to as a double pointer.
Let us illustrate with an example. Consider the following code, and imagine that the form containing this code has two textboxes: TextBox1 with text "TextBox1" and TextBox2 with text "TextBox2":
Now, here is what happens when we execute DoIt. Note that the argument is passed to GetText by value in this case.
Now suppose we change the ByVal keyword to ByRef in GetText. Here is what happens:
In VB .NET, parameters can be declared as optional using the Optional keyword, as shown in the following code:
In VB .NET, all optional parameters must declare a default value, which is passed to the procedure if the calling program does not supply that parameter.
The following rules apply to optional arguments:
Note that in earlier versions of VB, you could omit the default value and, if the parameter was of type Variant, you could use the IsMissing function to determine if a value was supplied. This is not possible in VB .NET, and the IsMissing function is not supported.
Normally, a procedure definition specifies a fixed number of parameters. However, the ParamArray keyword, which is short for Parameter Array, permits us to declare a procedure with an unspecified number of parameters. Therefore, each call to the procedure can use a different number of parameters. Suppose, for instance, that we want to define a function to take the average of a number of test scores, but the number of scores may vary. Then we declare the function as follows:
Now we can make calls to this function with a varying number of arguments
The following rules apply to the use of ParamArray:
VB.NET Related Interview Questions
|VB.NET Interview Questions||ASP.NET Interview Questions|
|ADO.Net Interview Questions||Windows Presentation Foundation(WPF) Interview Questions|
|Windows CE .NET Interview Questions||Dot Net Framework Interview Questions|
|Asp Dot Net Mvc 4 Interview Questions||Asp Dot Net Mvc Interview Questions|
Variables And Data Types
Introduction To Object-oriented Programming
The .net Framework: General Concepts
The .net Framework Class Library
Delegates And Events
Error Handling In Vb .net
The Language Reference
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.