Functions that return more than 1 value - Such a beast?

  • Thread starter Thread starter Daren Hawes
  • Start date Start date
D

Daren Hawes

Hi,

I am creating a function for a payment gateway and wondered is there a
way for a function to return 2 or more values.

IE

Function Payment(ByVal ccNumber as String, Byval Amount as Integer) as
String AND Something else!

....
....
Return (Return Status - FAILED)
Return (Reason)
....
....
End Function


Or do I have to use properties?

Thx Daren
 
Hi Daren

You could return a structure or a class object, and then fill in the
elements/properties accordingly.

HTH

Charles
 
Hi Daren,

I choise for the method from Charles, however you can even do it using a
simple arraylist.

Cor
 
You could also use ByRef variables.

eg.
Enum eReasons
Passed = 0
Failed = 1
Other =2
end Enum
function Payment(byval ccNumber as String, byval Amount as Integer, _
byref lReason as eReasons, byref sReason as String)

dim lReason as eReasons
dim sReason as String

Call Payment("12345678901234567890", 100, lReason, sReason)

However, as far as I know, this works only for value types.
Ofcourse, if you have multiple values, its better to go with a structure or
object as mentioned earlier..

Imran.
 
Daren Haweswrote:
Hi,
I am creating a function for a payment gateway and wondered is there a
way for a function to return 2 or more values.

IE

Function Payment(ByVal ccNumber as String, Byval Amount as Integer) as
String AND Something else!

....
....
Return (Return Status - FAILED)
Return (Reason)
....
....
End Function


Or do I have to use properties?

Thx Daren

I think a good rule of thumb is to use a class or structure as others
have indicated, if the data you want returned can be combined into
some intuitive entity. If not, then use multiple ByRef parameters.
 
Daren,
If I used ByRef parameters as the others suggest to return multiple values
from a method, I would seriously consider NOT making it a function, as this
can cause side effects that other programmers using your function are not
aware of.
Return (Return Status - FAILED)
Suggests that you are "returning" an error or exception. If "return status
failed" is really an indication that the process can not be completed! I
would consider throwing an Exception at this point, as returning a "failed
status" can other other programmers using your function to easily ignore the
"failed status" and continue process, where as an Exception, they at least
need to wrap your function in a Try/Finally to ignore the Exception.

Hope this helps
Jay
 
Jay B. Harlow said:
I would consider throwing an Exception at this point, as returning a "failed
status" can other other programmers using your function to easily ignore the
"failed status" and continue process, where as an Exception, they at least
need to wrap your function in a Try/Finally to ignore the Exception.

I =definitely= second that motion! Troubleshooting is a whole lot easier
this way.

Best Regards,

Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top