How do you return multiple values in a function?

Recommended Answers

All 12 Replies

That depends. Are the values all of the same type? If so you can have the function return a typed list:

Public Function MultipleReturn() As List(Of String)
       Dim places As New List(Of String)
        places.Add("A")
        places.Add("B")
        places.Add("C")
        places.Add("D")
return places

A list or ListArray can be returned from a function as Ryshad suggests above. Another option is you can create a user defined structure and pass that back from your function.

Public Structure Student
    Public Name As String
    Public Age As Integer
    Public GradePointAverage As Decimal
End Structure

Public Function GetStudent() as Student
     Dim udtNew As Student
    
    udtNew.Name = "Johny"
    udtNew.Age = 12
    udtNew.GradePointAverage = 3.5

    Return udtNew
     
End Funtion

A list or ListArray can be returned from a function as Ryshad suggests above. Another option is you can create a user defined structure and pass that back from your function.

Public Structure Student
    Public Name As String
    Public Age As Integer
    Public GradePointAverage As Decimal
End Structure

Public Function GetStudent() as Student
     Dim udtNew As Student
    
    udtNew.Name = "Johny"
    udtNew.Age = 12
    udtNew.GradePointAverage = 3.5

    Return udtNew
     
End Funtion

Thats what I did actually, I was trying to see if there are other easier alternatives.

Thanks anyway.

It depends on the data that your sending back but I was thinking you might also be able to return the info in a dataset or datarow.

It depends on the data that your sending back but I was thinking you might also be able to return the info in a dataset or datarow.

I was actually trying to retrieve values from a listview, I used defined structure to return values.

I used array for each column in listview.

Sounds perfect for records in a datatable rather then a seperate array for each column.

Sounds perfect for records in a datatable rather then a seperate array for each column.

Please enlighten me.

You can create a dataset/datatable and add a column for each field you are currently using as a seperate array. You can then add, edit, delete records as needed. A datatable will give additional advantages such as giving you search, filtering and sorting capabilites. You would also have the options to create relations between tables if needed. Such as if column1 (student) has many column2 (classes) you can easily define two seperate tables within the dataset and automatically link the records defining a relationship. The end result is simply passing back the dataset/datatable from the function. Also with a single call you can read/write all the data to an xml file.

Looks like I'll be switching to Datagrid instead of Listview.

Thanks

Can a datagrid adds columns and edit its properties at runtime?

A DataGridView control is simply a means of displaying the data from its datasource (dataset or datatable). Even if changing and storing your data in a DataSet, you can still display the data in a ListView if you choose too.

To answer your actual question, yes you can dynamically add columns to your "DataTable" that will then appear in your DataGridView. And yes editing records is very easy and I will be here to help :)

I am pleased to learn that you can actually fill the DataGrid with just one function, as well as sort/search/filter.

DO you have a basic tutorial on connecting, retrieving, and filling data to a datagrid from a mySQL?

Oh you didnt even mention the data was database driven, the datasets will definitely be of great value. With a database I can simply drag & drop the tables I want into a typed dataset and the matching table structures are automatically generated. Retrieving (also inserting, updating & deleting) the data is merely a matter of adding the queries to TableAdapters or DataAdapters and calling them. My personal preference though is not to put any hard coded queries within my program(s) and instead put them right into Stored Procedures in the database and then just call the SP from the program adapters.

I'll put some Ado.Net tutorial links that should get you started. If your interested in books, I have some recommendations.

(no particular order in which to start with)
ADO.Net
Data Walkthroughs 1
Data Walkthroughs 2

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.