Returning multiple values from a function

Using a loop in a function to return multiple values

Page 1 of 1

4 Replies - 39076 Views - Last Post: 18 March 2009 - 10:04 AM Rate Topic: -----

#1 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Returning multiple values from a function

Post icon  Posted 17 March 2009 - 08:56 PM

Hello there ,



Ok so I am working on a project I would like to use a function in a class I created to return multiple values in order to populate a listbox. I wanted to use a do until loop to achieve this. Rather than post the actual code which has a bunch of variables and whatnot that would not help in clarifying my problem. Instead I will just post a simplified example of what I am doing :
Public Function Listboxfiller

Dim astring As String = "This is a string"
Dim Counter as integer = 0

Do until Counter = 5

return astring

Counter += 1

Loop



Then on the form I would do something like :
lstListbox.Items.Add(Myclass.Listboxfiller)




What happens right now is that the listbox only gets the first value, when I ran through in debug mode I found the loop only runs once, even though it should go multiple times.

I am VERY much a programming noob, this is the first class I have ever made and I am sure I am screwing a few things up :P . Can anyone offer any suggestions as to what is causing this to happen? Can functions only return once? Why would the loop terminate prematurely like that? Should I be using a procedure instead of a function? Any advice would be greatly appreciated! Thanks much !

Is This A Good Question/Topic? 0
  • +

Replies To: Returning multiple values from a function

#2 myork  Icon User is offline

  • D.I.C Head

Reputation: 41
  • View blog
  • Posts: 129
  • Joined: 04-March 09

Re: Returning multiple values from a function

Posted 17 March 2009 - 09:29 PM

There are two ways I can see for going about this easily, you can either return a Listbox.ObjectCollection from your function, or use an array. Either of these solutions will allow you to call your function from directly inside a lstListBox.items.addRange(yourFunction()). Something like either of the following should work:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		Dim l As New ListBox
		l.Items.AddRange(GenerateListItems())
		l.Items.AddRange(FillLB(l))
	End Sub

	Public Function GenerateListItems() As Object()
		Dim stuff(5) As Object
		For i As Integer = 0 To 5
			stuff(i) = i
		Next
		Return stuff
	End Function

	Public Function FillLB(ByVal lb As ListBox) As ListBox.ObjectCollection
		Dim n As New ListBox.ObjectCollection(lb)
		For i As Integer = 0 To 5
			n.Add(i)
		Next
		Return n
	End Function


This post has been edited by myork: 17 March 2009 - 09:30 PM

Was This Post Helpful? 1
  • +
  • -

#3 mrmcpott  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 95
  • Joined: 24-November 08

Re: Returning multiple values from a function

Posted 18 March 2009 - 06:44 AM

View PostJstall, on 17 Mar, 2009 - 07:56 PM, said:

Hello there ,



Ok so I am working on a project I would like to use a function in a class I created to return multiple values in order to populate a listbox. I wanted to use a do until loop to achieve this. Rather than post the actual code which has a bunch of variables and whatnot that would not help in clarifying my problem. Instead I will just post a simplified example of what I am doing :

Dim Counter As Integer = 0
Public Function Listboxfiller

Dim astring As String = "This is a string"
Dim Counter as integer = 0

Do until Counter = 5

return astring

Counter += 1

Loop



Then on the form I would do something like :
lstListbox.Items.Add(Myclass.Listboxfiller)




What happens right now is that the listbox only gets the first value, when I ran through in debug mode I found the loop only runs once, even though it should go multiple times.

I am VERY much a programming noob, this is the first class I have ever made and I am sure I am screwing a few things up :P . Can anyone offer any suggestions as to what is causing this to happen? Can functions only return once? Why would the loop terminate prematurely like that? Should I be using a procedure instead of a function? Any advice would be greatly appreciated! Thanks much !


Try declaring your Counter outside of the function, perhaps at the top of the class. Each time the function executes it redeclares the Counter, resetting its value to 0.

Dim Counter As Integer ' Try placing the counter here.
Public Function Listboxfiller

Dim astring As String = "This is a string"
' Dim Counter as integer = 0 (This is where you had the counter).

Do until Counter = 5

return astring

Counter += 1

Loop


Was This Post Helpful? 1
  • +
  • -

#4 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Returning multiple values from a function

Posted 18 March 2009 - 08:47 AM

Went with the array and the addrange method(which I was unaware of) and it worked fine :D thanks much for the advice from both of you! I very much appreciate it!
Was This Post Helpful? 0
  • +
  • -

#5 mrmcpott  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 95
  • Joined: 24-November 08

Re: Returning multiple values from a function

Posted 18 March 2009 - 10:04 AM

View PostJstall, on 18 Mar, 2009 - 07:47 AM, said:

Went with the array and the addrange method(which I was unaware of) and it worked fine :D thanks much for the advice from both of you! I very much appreciate it!


You are welcome :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1