How to return multiple value from a function in VB 6.0

Any suggestion sir/madam?

Recommended Answers

All 4 Replies

How about arrays?

Private Function ReturnArrays(SomeArgs As String) As String()
   'Code goes here
End Function

You can also use user define types and even classes.

@jhai salvador

Thanks for replying, Yes, it can be done using array. I have tried like this

Form1

Private Sub Command1_Click()
Text2.Text = ReturnPrevCurNxt(Val(Text1.Text))(0)
Text3.Text = ReturnPrevCurNxt(Val(Text1.Text))(1)
Text4.Text = ReturnPrevCurNxt(Val(Text1.Text))(2)
End Sub

Module1

Function ReturnPrevCurNxt(x As Integer)
 Dim arr(0 To 2) As Double
 arr(0) = x - 1
 arr(1) = x
 arr(2) = x + 1
 ReturnPrevCurNxt = arr
End Function

But, I have not tried using user define type or classes yet.

This an example about struct of employee combining with array

Module1

Public Type Employee
    EmpNo As Integer
    EmpName As String
    EmpPhone As String
End Type

Public Sub SetEmployee(ByRef emp() As Employee, ByVal i As Integer)
    ReDim emp(i)
End Sub

Public Sub SetEmpData(ByRef emp() As Employee, ByVal i As Integer, ByVal no As Integer, ByVal name As String, ByVal phone As String)
    emp(i).EmpNo = no
    emp(i).EmpName = name
    emp(i).EmpPhone = phone
End Sub

'GetEmpData will return array containing employee details
Public Function GetEmpData(ByRef emp() As Employee, ByVal i As Integer) As Employee
    GetEmpData = emp(i)
End Function

Form1
All inputs will use inputbox and just add two button for set and get employee details

Dim NewEmp() As Employee ' declare array as struct of employee
Dim i As Integer

Private Sub Command1_Click()
   ' how many employee to add
    temp = InputBox("How many employee ?")
    SetEmployee NewEmp, Int(temp - 1)

    ' add employee detail
    For i = 0 To UBound(NewEmp)
        EmpNos = InputBox("Emp " & i + 1 & " No")
        EmpNames = InputBox("Emp " & i + 1 & " Name")
        EmpPhones = InputBox("Emp " & i + 1 & " phone")

        SetEmpData NewEmp, i, EmpNos, EmpNames, EmpPhones
    Next
End Sub

Private Sub Command2_Click()
' Extract detail of employee for each array
For i = 0 To UBound(NewEmp)
    MsgBox GetEmpData(NewEmp, i).EmpNo & "," & GetEmpData(NewEmp, i).EmpName & "," & GetEmpData(NewEmp, i).EmpPhone
Next
End Sub
commented: Very good example... +4
commented: Very helpful +2

JX_man, thanks for your example sir.

I have done with my previous example. Its working nicely.

Module1

Type UDT
    a As Integer
    b As Integer
    c As Integer
End Type

Public Function ReturnPrevCurNxt(x As Integer) As UDT
ReturnPrevCurNxt.a = x - 1
ReturnPrevCurNxt.b = x
ReturnPrevCurNxt.c = x + 1
End Function

form1

Private Sub Command1_Click()
Text2.Text = ReturnPrevCurNxt(Val(Text1.Text)).a
Text3.Text = ReturnPrevCurNxt(Val(Text1.Text)).b
Text4.Text = ReturnPrevCurNxt(Val(Text1.Text)).c
End Sub

So, I am going to closed this thread as it has solved.

Thank you all

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.