Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))

Problem

You need to pass complex data types to a function, or return an equally complex type.

Solution

Just pass the data. Complex data in .NET is really no different from simple data in how it is passed to or returned from functions.

Discussion

Arrays are probably the most cumbersome, only because you have to add two extra characters in the function definition. The following function definition accepts an Integer array and returns a related String array:

Public Function ConvertIntArrayToString( _ ByVal origArray() As Integer) As String( ) ' ----- Take a basic Integer array, and return a ' String equivalent. Dim newArray(UBound(origArray)) As String For counter As Integer = 0 To UBound(origArray) newArray(counter) = CStr(origArray(counter)) Next counter Return newArray End Function

In some non-.NET languagesincluding earlier versions of Visual Basicit is not always obvious how you pass complex data types, such as complete arrays, into and out of functions. In .NET, it's a snap. All complex data typesinstances of structures and classesare simple variables that can be passed freely through arguments or return values. An array is a standard reference type, even if it contains value type elements.

Категории