1. Home
  2. Computing & Technology
  3. Visual Basic

Procedures in VB.NET
Parameters

By Dan Mabbutt, About.com

Passing Parameters

Earlier, I mentioned that there were complications: "What if the parameter that is passed is an array or a collection?" I'm happy to report that arrays and collections are passed without problems. But you do have to get the syntax right, so here's an example of both. Just to prove the flexibility of VB.NET, the array example passes a multidimensional array.

Dim arrayArg(,) As Integer = _
   {{1, 2, 3, 4}, {9, 8, 7, 6}}
s1(arrayArg)
Dim collArg As New Collection
collArg.Add("AAAA")
collArg.Add(1111)
collArg.Add("BBBB")
collArg.Add(2222)
s2(collArg)

Sub s1(ByRef arrayParam(,) As Integer)
   Dim i, j As Integer
   For i = 0 To arrayParam.GetLength(0) - 1
      For j = 0 To arrayParam.GetLength(1) - 1
         Debug.Write(arrayParam(i, j).ToString)
         If j <> arrayParam.GetLength(1) - 1 Then Debug.Write(" -- ")
      Next
      Debug.WriteLine(" ")
   Next
End Sub
Sub s2(ByVal collParam As Collection)
   Dim i As Integer = 0
   Dim j As Integer = collParam.Count
   For Each collItem As Object In collParam
      i += 1
      Debug.Write(collItem.ToString)
      If i <> j Then Debug.Write(" -- ")
   Next
End Sub

Passing Optional Parameters or a Variable Array of Parameters

You can use the ParamArray keyword to pass an arbitrary number of arguments. ParamArray was also available in VB 6 and its use is similar in VB.NET. The number of elements passed in the ParamArray is set each time the procedure is called.

Passing optional parameters must be done in the procedure definition like this:

Sub subName(ByVal parm1 As datatype1, _
   Optional ByVal parm2 As datatype2 = default value, _
   Optional ByVal parm3 As datatype3 = default value, _
   .... for as many optional parameters as you need ...
   )

The optional parameters have to be the last ones in the parameter list. You can then call the procedure like this:

Call subName(arg1, ,arg2, ... for all optional arguments ..., )

If an optional argument isn't passed, the default value is passed instead. This makes it possible for this approach to create a problem in deciding whether a parameter was actually passed in a case where it's possible to pass the default. For example, if any integer might be passed, then whatever you pick as a default might be passed either because it was omitted or because it happened to be the real value. In a case like this, you can pass optional arguments by using VB.NET overloading too. Just code different overloaded procedures for each possible optional parameter. That way, you can be sure whether a parameter was actually passed or not. The only real problem here is that you might have to code a lot of procedures if you have a lot of optional parameters. If you have N optional parameters, you'll need 2 to the Nth power procedures. Here's an example where two optional parameters are passed using overloaded procedures. Notice that I need four overloaded subroutines.

Dim parm1 As String = "AAAA"
Dim parm2 As Integer = 12345
testSub()
testSub(parm1)
testSub(parm2)
testSub(parm1, parm2)

Overloads Sub testSub()
   Debug.WriteLine("nothing passed")
End Sub
Overloads Sub testSub(ByVal i As String)
   Debug.WriteLine(i)
End Sub
Overloads Sub testSub(ByVal i As Integer)
   Debug.WriteLine(i.ToString)
End Sub
Overloads Sub testSub(ByVal i As String, ByVal j As Integer)
   Debug.WriteLine(i & " -- " & j.ToString)
End Sub

Generic Procedures

A completely new type of procedure that was just added in VB.NET 2005 is called a generic procedure. This new capability is so important that I have written a complete article, "Generics! Cleaner Data - Faster Code!", just to cover it. In that article I write, "In true OOP fashion, generics help you write classes (or, use classes that have already been written) for more purposes and avoid having to write essentially the same code with just the datatype changed when the rest of the processing is the same."

Operator Procedures

In VB.NET, you can not only overload (that is, redefine with different parameters) your own procedures, you can also overload a VB.NET standard operator such as multiplication ("*") or not equal ("<>"). You don't run into this type of programming every day and it's questionable that it's really very useful. But it's there if you need it! Since this type of procedure isn't likely to be one that you use all the time (and this article is getting a bit long anyway), I'll refer you to MSDN for complete specs on this.

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Learn VB.NET
  5. Procedures in VB.NET

©2009 About.com, a part of The New York Times Company.

All rights reserved.