Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Returning more than 1 value from a function

Returning more than 1 value from a function

Scheduled Pinned Locked Moved Visual Basic
questioncsharpdatabasegame-dev
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xBlitzerx
    wrote on last edited by
    #1

    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

    R 1 Reply Last reply
    0
    • X xBlitzerx

      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

      R Offline
      R Offline
      Ray Cassick
      wrote on last edited by
      #2

      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_name

      End 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 userResponse

      Now 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 Sub

      Hope 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."


      X 1 Reply Last reply
      0
      • R Ray Cassick

        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_name

        End 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 userResponse

        Now 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 Sub

        Hope 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."


        X Offline
        X Offline
        xBlitzerx
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups