Returning more than 1 value from a function
-
Ok im using VB.net, a console app. I am making a database that stores games(members also, name, genre, rating) i'm dividing the main code into some sub procdures and functions for easier readability. So i have one sub for displaying the main menu(add, view..etc) But now i wanna make a function to add a game to the array... so i ask the user for the info, but how do i return all the values seperatly?? i tried assigning them directly to the array(gameArray.name = console.readline()), but it doesnt work... Any ideas will help!! ______________________________________________ "I'm not me when I dream...anymore." -TRUSTcompany
-
Ok im using VB.net, a console app. I am making a database that stores games(members also, name, genre, rating) i'm dividing the main code into some sub procdures and functions for easier readability. So i have one sub for displaying the main menu(add, view..etc) But now i wanna make a function to add a game to the array... so i ask the user for the info, but how do i return all the values seperatly?? i tried assigning them directly to the array(gameArray.name = console.readline()), but it doesnt work... Any ideas will help!! ______________________________________________ "I'm not me when I dream...anymore." -TRUSTcompany
It sounds like you want to build a function that askes the user for three items and then returns those items so they can be used in anohter part of the program... Buld a structure as so and declare it outside the function:
Public Structure InputData
Dim arg1 As type_name
Dim arg2 As type_name
Dim arg3 As type_nameEnd Structure
In your function create an instance of the structure, get all your input and assign each piece of input to each structure member:
Public Function GetInput() as InputData
Dim userResponse as InputData'Get your input here
.
.
.
'assign each pice of input to your structure members
userResponse.arg1 = inputValue1
userResponse.arg2 = inputValue2
userResponse.arg3 = inputValue3'return your data from the function
Return userResponseNow you can call the function as so:
Dim returnData as InputData
returnData = GetInput()From here if you want to pass it to another function to write the data to a DB you can simply do this:
Public Sub WriteDataToDB(inData As InputData)
'open the DB
'write each value to the DB
'you access each argument by the following
inData.arg1
inData.arg2
inData.arg3
.
.
.
End SubHope that helps and I hope there are few typos :-D
Paul Watson wrote: "At the end of the day it is what you produce that counts, not how many doctorates you have on the wall."
George Carlin wrote: "Don't sweat the petty things, and don't pet the sweaty things." Unknown wrote: "I love long walks, especialy taken by those that annoy me."
-
It sounds like you want to build a function that askes the user for three items and then returns those items so they can be used in anohter part of the program... Buld a structure as so and declare it outside the function:
Public Structure InputData
Dim arg1 As type_name
Dim arg2 As type_name
Dim arg3 As type_nameEnd Structure
In your function create an instance of the structure, get all your input and assign each piece of input to each structure member:
Public Function GetInput() as InputData
Dim userResponse as InputData'Get your input here
.
.
.
'assign each pice of input to your structure members
userResponse.arg1 = inputValue1
userResponse.arg2 = inputValue2
userResponse.arg3 = inputValue3'return your data from the function
Return userResponseNow you can call the function as so:
Dim returnData as InputData
returnData = GetInput()From here if you want to pass it to another function to write the data to a DB you can simply do this:
Public Sub WriteDataToDB(inData As InputData)
'open the DB
'write each value to the DB
'you access each argument by the following
inData.arg1
inData.arg2
inData.arg3
.
.
.
End SubHope that helps and I hope there are few typos :-D
Paul Watson wrote: "At the end of the day it is what you produce that counts, not how many doctorates you have on the wall."
George Carlin wrote: "Don't sweat the petty things, and don't pet the sweaty things." Unknown wrote: "I love long walks, especialy taken by those that annoy me."
Thanks Ray that kinda helps... but i'm still not sure why i'm doing that... Maybe i didnt explain it good enought.. I making an array, that has 3 members (name, genre, rating) Overall i want a little menu 1) Add a game 2) Display a game 3) Show average rating of games 4) Quit Something like that..so first i made a sub prodecure that displayed this menu (so i can call it after each option the user makes), that was all good So the sub main looks like this so far Private Sub Main() Dim gamesArray() as Game 'Game is a declared structure above sub main) Dim choice as integer call DisplayMainMenu() choice = console.readline() End Sub So then I started to do this: Private Sub Main() Dim gamesArray() as Game 'Game is a declared structure above sub main) Dim choice as integer call DisplayMainMenu() choice = console.readline() If choice = 1 then * call AddGame() End if End Sub Private Function AddGame() console.write("Enter the game name: ") name = console.readline() console.write("Enter the genre: ") genre = console.readline() console.write("Enter the games rating: ") rating = console.readline() End Function *** So my question being, if i get the info for the game in the function.. i somehow need to get all 3 pieces back up into the array... Hope this clarifies things...:-D EDIT: Sorry forgot the IF ______________________________________________ "I'm not me when I dream...anymore." -TRUSTcompany