Function that returns more than 1 value

Rate It (1)

Last post 04-25-2009 11:28 PM by Hawkx1. 11 replies.

Sort Posts:

  • Function that returns more than 1 value

    03-28-2007, 3:56 AM
    • Participant
      1,123 point Participant
    • whisky
    • Member since 09-25-2003, 4:52 AM
    • Posts 487

    Hi,

    I have a stored procedure and a class in vb.net 2.0. How can I return more than one value to the function?

     

     

  • Re: Function that returns more than 1 value

    03-28-2007, 4:15 AM

    Hi, friend:

      To return more than one value in vb.net, you should use a parameter to carry the extra value returned. Just set byref instead of byval before parameters.

    Regards

    Sincerely,
    Allen Chen
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Function that returns more than 1 value

    03-28-2007, 4:28 AM
    Answer
    • Star
      12,284 point Star
    • Mr^B
    • Member since 02-12-2006, 11:38 AM
    • Posts 2,177
    Or you could return a List object or an Array with all the values in that you want to return.  Or concatenate the different values into a single string  and then split the string once returned.  There are many many ways to implement this, it all depends on what it is you are trying to achieve and how it fits with the rest of the code (in terms of scalability and maintainability).
    MCSD.Net
  • Re: Function that returns more than 1 value

    03-28-2007, 4:38 AM
    • Participant
      1,123 point Participant
    • whisky
    • Member since 09-25-2003, 4:52 AM
    • Posts 487

    Thanks for the replies.

    Here is my function in my class and i want to return both the values of Active and Role to my code .

    Public Function GetUsers(ByVal UserName As String) As Integer

    myConnection.Close()

    Dim myCommand As New SqlCommand("GetUsersByUsername", myConnection)

    myCommand.CommandType = CommandType.StoredProcedure

    myCommand.Parameters.AddWithValue(

    "@USERNAME", UserName)

    myConnection.Open()

    Dim reader As SqlDataReader = myCommand.ExecuteReader()

    Do While reader.Read

    Role = reader.Item(

    "roleid")

    Active = reader.Item(

    "Active")

    Loop

    Return Role

    reader.Close()

    myConnection.Close()

    End Function
  • Re: Function that returns more than 1 value

    03-28-2007, 4:43 AM
    • Participant
      1,123 point Participant
    • whisky
    • Member since 09-25-2003, 4:52 AM
    • Posts 487

    Thanks for the replies.

    Here is my function in my class and i want to return both the values of Active and Role to my code .

    Public Function GetUsers(ByVal UserName As String) As Integer

    myConnection.Close()

    Dim myCommand As New SqlCommand("GetUsersByUsername", myConnection)

    myCommand.CommandType = CommandType.StoredProcedure

    myCommand.Parameters.AddWithValue(

    "@USERNAME", UserName)

    myConnection.Open()

    Dim reader As SqlDataReader = myCommand.ExecuteReader()

    Do While reader.Read

    Role = reader.Item(

    "roleid")

    Active = reader.Item(

    "Active")

    Loop

    Return Role

    reader.Close()

    myConnection.Close()

    End Function
  • Re: Function that returns more than 1 value

    03-28-2007, 4:50 AM

    Hi:

      Where is the definition of Role and Active? Are they both global variables?

    Regards

    Sincerely,
    Allen Chen
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Function that returns more than 1 value

    03-28-2007, 5:05 AM
    • Participant
      1,123 point Participant
    • whisky
    • Member since 09-25-2003, 4:52 AM
    • Posts 487

    Public

    Class UsersClass

     

    Public Shared Role As Integer

    Public Shared Active As Integer

    Public Function GetUsers(ByVal UserName As String) As Integer

    myConnection.Close()

    Dim myCommand As New SqlCommand("GetUsersByUsername", myConnection)

    myCommand.CommandType = CommandType.StoredProcedure

    myCommand.Parameters.AddWithValue(

    "@USERNAME", UserName)

    myConnection.Open()

    Dim reader As SqlDataReader = myCommand.ExecuteReader()

    Do While reader.Read

    Role = reader.Item(

    "roleid")

    Active = reader.Item(

    "Active")

    Loop

     

    Return Role

    reader.Close()

    myConnection.Close()

    End Function

    Private ReadOnly Property ConnectionString() As String

    Get

    Return ConfigurationManager.ConnectionStrings("ChildbirthClassesConnectionString").ConnectionString

    End Get

    End Property

    End

    Class
  • Re: Function that returns more than 1 value

    03-28-2007, 5:12 AM

    Hi:

      Then, just as I mentioned,

    Public Function GetUsers(ByVal UserName As String, ByRef result_active As String) As Integer

    and in the end of the function:

    result_active=Active

     

    Regards

     

    Sincerely,
    Allen Chen
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Function that returns more than 1 value

    03-28-2007, 8:51 AM
    • Participant
      1,123 point Participant
    • whisky
    • Member since 09-25-2003, 4:52 AM
    • Posts 487

    Public Function GetUsers(ByVal UserName As String, ByRef result_active As Integer, ByRef result_role As Integer) As Integer

    myConnection.Close()

    Dim myCommand As New SqlCommand("GetUsersByUsername", myConnection)

    myCommand.CommandType = CommandType.StoredProcedure

    myCommand.Parameters.AddWithValue(

    "@USERNAME", UserName)

    'myCommand.Parameters.AddWithValue("@password", Password)

    myConnection.Open()

    Dim reader As SqlDataReader = myCommand.ExecuteReader()

    Do While reader.Read

    Role = reader.Item(

    "roleid")

    Active = reader.Item(

    "Active")

    Loop

    'Return Role

    reader.Close()

    myConnection.Close()

    result_role = Role

    result_active = Active

     

    I have to get the results of both result_role  and result_active in my code (web form). How do i get the value of both variables?

  • Re: Function that returns more than 1 value

    03-28-2007, 9:08 PM
    Answer

    Hi:

    Your Function:

    Public Function GetUsers(ByVal UserName As String, ByRef result_active As Integer, ByRef result_role As Integer) As Integer

    .....

    Role = reader.Item(

    "roleid")

    result_role =Role

    Active = reader.Item(

    "Active")

    result_active =Active

    .....

    End Function

    Get results:

    Dim  rRole As Integer

     

    Dim rActive As Integer 
    Dim UserName As String

    GetUsers(UserName,rActive, rRole)

    Regards

    Sincerely,
    Allen Chen
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Function that returns more than 1 value

    04-21-2009, 5:06 PM
    • Member
      6 point Member
    • geordiejenner
    • Member since 02-28-2008, 2:40 PM
    • Posts 3

    Hi Allen

    i cannot get this to work.  i am using wcf and a SqlDataReader, the function returning a list(of and i added a byref to return another listof, but it returns nothing.  when i step thru the function on the server side of the service, the data is being populated.

    any ideas would be appreciated

  • Re: Function that returns more than 1 value

    04-25-2009, 11:28 PM
    • Member
      3 point Member
    • Hawkx1
    • Member since 04-23-2009, 1:05 PM
    • Posts 11

    This is kinda on the same vein i am using a web service that I created that recieves a zip Code as String, ErrorCheck as Boolean and ErrorMessage as String i set it up like this

        Public Function ZipValidate(ByVal Zip As String, ByVal ErrorCheck As Boolean, ByVal ErrorMessage As String) As String

            Dim City As String
            Dim State As String

            Select Case Zip
                Case "20193"
                    City = "Reston"
                    State = "VA"
                Case "22219"
                    City = "Arlington"
                    State = "VA"
                Case "21201"
                    City = "Baltimore"
                    State = "MD"
                Case "21401"
                    City = "Annapolis"
                    State = "MD"
                Case "20810"
                    City = "Bethesda"
                    State = "MD"
                Case "20001"
                    City = " Washington"
                    State = "DC"
                Case "15235"
                    City = "Pittsburg"
                    State = "PA"
                Case "191192"
                    City = "Philadelphia"
                    State = "PA"
                Case "10165"
                    City = "New York"
                    State = "NY"
                Case "14201"
                    City = "Buffalo"
                    State = "NY"
                Case Else
                    City = ""
                    State = ""
                    ErrorCheck = False
                    ErrorMessage = "Zip Code has not been found"
            End Select

            Return City
            Return State


        End Function

    I need to return City and State, ErrorCheck and ErrorMessage to the wcf service that calls it but I am unsure how to do this please help me figure out how to fix my problem

Page 1 of 1 (12 items)