Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get my function in module to spit out values - HOW? 2

Status
Not open for further replies.

x508

Programmer
Jun 26, 2003
396
0
0
ZA
Hi, I know that it is possible with dll's, that when you "ask" them a question, they spit out an answer.

Can I do this with my functions in my module, or would I have to use Public variable in order to get the function results?

Thanks in advance

**********************************
May the Code Be With You...
----------
x50-8 (X Fifty Eigt)
 
OK, I now know how to get one value from the function.

Funtion MyFunct(Bla as string) As string

so MyFunct will hold the result of the function.

How do I get to spit out more than one value

**********************************
May the Code Be With You...
----------
x50-8 (X Fifty Eigt)
 
Instead of declaring the function as a string, you can use either an array or collection. Both would enable you to return multiple values.

Patrick
 
Here's a pointless example...

[blue]Option Explicit

Private Sub [/blue]Command1_Click()
[blue]Dim [/blue]i [blue]As Integer
Dim [/blue]arr() [blue]As String

[/blue]arr = SplitLetters("a|b|c|d|e|f|g", "|")
[blue]For [/blue]i = 0 [blue]To UBound[/blue](arr)
Me.[blue]Print [/blue]arr(i)
[blue]Next [/blue]i
[blue]End Sub

Public Function [/blue]SplitLetters(strArr [blue]As String[/blue], Delim [blue]As String[/blue]) As [blue]Variant
[/blue]SplitLetters = Split(strArr, Delim)
[blue]End Function
[/blue]
 
Thanks to both of you...

I am however gonna use my own Colletion to accomplish this.
You have given me a good starting point and my new Mast VB 6.0 book is starting to pay off!!!

Thanks again

**********************************
May the Code Be With You...
----------
x50-8 (X Fifty Eigt)
 
Hang on a moment.
How many variables do you need to return?

If it is less than (say )6, there is a much simpler method,making use of the difference between ByRef and ByVal when passing parameters in.

There is probably an FAQ somewhere, but here is (I hope) a simple example of getting two values back from a function or subroutine (!)

Consider this:

Dim x as long
x = 6
Call DoSomething(x)
debug.print x
'(result is 9!!!)

Private Sub DoSomething( p as integer)
p=9
end sub

..if you try that you will find that x has a different value after the call.
This is because VB defaults to passing parameters ByRef - the equivalent of using pointers in C

In fact, to prevent this sort of thing, you have to EXPLICITY tell VB not to allow the values of parameters to change.

This is done using the ByVal condition.

Thus:

Dim x as long
x = 6
Call DoSomething(x)
debug.print x
'(result is that x is still 6 as you expected)

Private Sub DoSomething( ByVal p as integer)
p=9
end sub




So, to get multiple values back from a function of subroutine, try this sort of thing:


Dim inputVal as integer
Dim outputA as integer
Dim outputB as integer
Dim outputC as integer

inputVal = 12

Call Multival(inputVal, outputA,outputB,outputC)

debug.print outputA,outputB,outputC




Private sub MultiVal (inp as integer,A as integer,B as integer, C as integer)

A=inp * 2
B=inp * 3
C=inp * 4
End Sub


 
Quite so. I'm with Jeff on this.

One nice approach (and it is used a lot by the Windows API, although sadly not consistently...), is to use [out]* parameters for the results that you want, and use the return value of the function to indicate either success or failure (rather than using a simple True/False result, it can be better to return Success/Error Code)

* If you use MSDN to look up Windows API calls you will see each of the parameters for the function calls classified as [in] or [out]. If you are looking to 'translate' API cals from C/C++ to VB, then these are often a clue as to whether the parameter should be declared as ByVal or ByRef
 
Excellent

Thanks Jeff and strong-one

This is a briliant solution.

My collections also worked, but this would do even better.
almost like working wiht an activeX dll?

**********************************
May the Code Be With You...
----------
x50-8 (X Fifty Eigt)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top