Recipe 3.11. Passing and Returning Structures and Other Objects


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.




Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net