Functions are not limited to returning simple data types such as integers or strings. They might return custom data types and even arrays. The capability of functions to return all types of data makes them very flexible and can simplify coding, so we’ll explore it in detail in the following sections. Using complex data types, such as structures and arrays, allows you to write functions that return multiple values.
Suppose you need a function that returns a customer’s savings and checking account balances. So far, you’ve learned that you can return two or more values from a function by supplying arguments with the ByRef keyword. A more elegant method is to create a custom data type (a structure) and write a function that returns a variable of this type.
Here’s a simple example of a function that returns a custom data type. This example outlines the steps you must repeat every time you want to create functions that return custom data types:
1. Create a new project and insert the declarations of a custom data type in the declarations section of the form:
Structure CustBalance
Dim SavingsBalance As Decimal
Dim CheckingBalance As Decimal
End Structure
Code language: VB.NET (vbnet)
2. Implement the function that returns a value of the custom type. In the function’s body, you must declare a variable of the type returned by the function and assign the proper values to its fields. The following function assigns random values to the fields CheckingBalance and SavingsBalance. Then assign the variable to the function’s name, as shown next:
Function GetCustBalance(ID As Long) As CustBalance
Dim tBalance As CustBalance
tBalance.CheckingBalance = CDec(1000 + 4000 * rnd())
tBalance.SavingsBalance = CDec(1000 + 15000 * rnd())
Return(tBalance)
End Function
Code language: VB.NET (vbnet)
3. Place a button on the form from which you want to call the function. Declare a variable of the same type and assign to it the function’s return value. The example that follows prints the savings and checking balances in the Output window:
Private Sub Button1 Click(...) Handles Button1.Click
Dim balance As CustBalance
balance = GetCustBalance(1)
Debug.WriteLine(balance.CheckingBalance)
Debug.WriteLine(balance.SavingsBalance)
End Sub
Code language: VB.NET (vbnet)
The code shown in this section belongs to the Structures sample project. Create this project from scratch, perhaps by using your own custom data type, to explore its structure and experiment with functions that return custom data types. In Chapter, “Building Custom Classes,” you’ll learn how to build your own classes and you’ll see how to write functions that return custom objects.
Example – The Types Project
The Types project (download project), demonstrates a function that returns a custom data type. The Types project consists of a form that displays record fields (see Figure below).
The Types project demonstrates functions that return custom data types.
Every time you click the Next button, the fields of the next record are displayed in the corresponding TextBox controls on the form. When all records are exhausted, the program wraps back to the first record.
The project consists of a single form and uses a custom data type, implemented with the following structure. The structure’s declaration must appear in the form’s code, outside any procedure, along with a couple of variable declarations:
Structure Customer
Dim Company As String
Dim Manager As String
Dim Address As String
Dim City As String
Dim Country As String
Dim CustomerSince As Date
Dim Balance As Decimal
End Structure
Private Customers(8) As Customer
Private cust As Customer
Private currentIndex As Integer
Code language: VB.NET (vbnet)
The array Customers holds the data for 10 customers, and the cust variable is used as a temporary variable for storing the current customer’s data. The currentIndex variable is the index of the current element of the array. The array is filled with Customer data, and the currentIndex variable is initialized to zero.
The Click event handler of the Next button calls the GetCustomer() function with an index value (which is the order of the current customer) to retrieve the data of the next customer, and displays the customer’s fields on the Label controls on the form with the ShowCustomer() subroutine. Then it increases the value of the currentIndex variable to point to the current customer’s index. You can open the Types project in Visual Studio and examine its code, which contains quite a few comments explaining its operation.