Results 1 to 2 of 2

Thread: VS2010 Return multiple types from functions

Hybrid View

  1. #1

    Thread Starter
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,686

    VS2010 Return multiple types from functions

    There are several ways to return multiple values from a function which range from using ByRef parameters in the function signature, arrays, classes, structures or working in tangent with global variables. VS2010 offers yet another alternate method using a new generic class called Tuple. Using Tuples if not careful as implemented in VS2010 can very well make code not easy to read/understand as the parameters can not be named or aliased. In general to return multiple values of the same type an array should be suffice as in figure 1. If you need to return multiple values of more than one type a structure (see figure 2) might be your cup of tea or a class (see figure 3). Creating the function in figure 2 and 3 without the structure or class defined VS IDE will provide an option to generate the type for you with various options so you need not have the classes already defined. On a side note, if the class will be used to populate say a DataGridView the order of the properties will also be the order of the DataGridView columns, no different than the order returned from an SQL statement.

    So where do Tuples fit in? One place might be where it is overkill to create a structure or class even though as stated above the class or structure can be auto-generated for you which would be my choice yet if you want you can create a function using Tuples that returns various types as shown in figure 4. Another usage might be to populate a DataGridView on the fly where normally in a loop we might use

    Code:
    DataGridView1.Rows.Add(New Object() {100, "Katrina Gallagher", #8/2/1988#})
    Or with Tuples which will make the row read-only
    Code:
    DataGridView1.Rows.Add(New Tuple(Of Integer, String, Date)(100, "Katrina Gallagher", #8/2/1988#))
    It is all about what fits your needs in the end. Personally with how easy it is to create stub classes this would be my method of choice to return more than one type from a function.

    Figure 1
    Code:
            Dim Values() As Integer = GetDateInformation_Array(Now)
            Console.WriteLine("Month: {0} - Day: {1} - Year: {2} - Week: {3}",
                              Values(0),
                              Values(1),
                              Values(2),
                              Values(3))
    
        Private Function GetDateInformation_Array(ByVal sender As Date) As Integer()
            Dim Result(3) As Integer
    
            Dim Week As Integer = DatePart(DateInterval.WeekOfYear,
                                           sender,
                                           FirstDayOfWeek.System,
                                           FirstWeekOfYear.FirstFullWeek)
    
            Result(0) = sender.Month
            Result(1) = sender.Day
            Result(2) = sender.Year
            Result(3) = Week
    
            Return Result
    
        End Function
    Figure 2
    Code:
            Dim StructInfo As DateStruct = GetDateInformation_Structure(Now)
            Console.WriteLine("Month: {0} - Day: {1} - Year: {2} - Week: {3}",
                              StructInfo.Month,
                              StructInfo.Day,
                              StructInfo.Year,
                              StructInfo.Week)
        Public Function GetDateInformation_Structure(ByVal sender As Date) As DateStruct
            Dim Result As New DateStruct
    
            Dim Week As Integer = DatePart(DateInterval.WeekOfYear,
                                           sender,
                                           FirstDayOfWeek.System,
                                           FirstWeekOfYear.FirstFullWeek)
    
            Result.Month = sender.Month
            Result.Day = sender.Day
            Result.Year = sender.Year
            Result.Week = Week
    
            Return Result
    
        End Function
    Figure 3
    Code:
            Dim ClassDateInfo As DateClass = GetDateInformation_Class(Now)
            Console.WriteLine("Month: {0} - Day: {1} - Year: {2} - Week: {3}",
                              ClassDateInfo.Month,
                              ClassDateInfo.Day,
                              ClassDateInfo.Year,
                              ClassDateInfo.Week)
        Public Function GetDateInformation_Class(ByVal sender As Date) As DateClass
            Dim Result As New DateClass
    
            Dim Week As Integer = DatePart(DateInterval.WeekOfYear,
                                           sender, FirstDayOfWeek.System,
                                           FirstWeekOfYear.FirstFullWeek)
    
            Result.Month = sender.Month
            Result.Day = sender.Day
            Result.Year = sender.Year
            Result.Week = Week
    
            Return Result
        End Function
    Figure 4
    Code:
            Dim TupleDemo = GetDetails(10)
            If TupleDemo.Item4 Then
                Console.WriteLine("First={0} Last={1} Age={2}",
                                  TupleDemo.Item1,
                                  TupleDemo.Item2,
                                  TupleDemo.Item3)
            End If
        Public Function GetDetails(ByVal Identifier As Integer) _
            As Tuple(Of String, String, Integer, Boolean)
    
            Select Case Identifier
                Case 10
                    Return Tuple.Create("Kevin", "Gallagher", 53, True)
                Case 20
                    Return Tuple.Create("Mary", "Hall", 50, True)
                Case Else
                    Return Tuple.Create("", "", 0, False)
            End Select
    
        End Function
    Last edited by kareninstructor; Aug 11th, 2011 at 07:56 AM.

  2. #2
    Member
    Join Date
    Jun 2010
    Posts
    55

    Re: VS2010 Return multiple types from functions

    Does anyone know I time when using tuples would be better used over classes or structures. I'd like to think time wasn't wasted developing a method that has know benefits over another.

    Kevin, Great explanation. Post rated.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width