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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using array in a procedure.

Status
Not open for further replies.

mgid

Programmer
Oct 2, 2000
87
US
I'm trying to load an array into a public array variable in one procedure tren use it in another and the procedure will not accept the array. I'm not sure how to go about doing this. Thanks for any help. Here's the gist of my code:

Public array1() As Double

Sub loadarray1()
....load array1()
End Sub

Sub mySub
myfunc(array1()) 'Error here "procedure or sub not defined".
End Sub
 
Hi,
Where and how do you define myfunc
e.g.
Public Sub myfunc(aar() As Double)

End Sub

Jon
 
Exactly like that in the same module. When debugging, the argument to the procedure is highlighted, not the procedure itself, so I'm assuming that when the procedure runs the argument is not recognized as a public array and is being recognized as another procedure(that does not exist). Is there any syntax that could flag the argument as an array rather than a procedure?
 
Try either:

myfunc array1()
Call myfunc(array1())

but not
myfunc(array1())

Jon
 
I tried those to no avail, but it worked when the array variable was qualified with the module name.

myfunc(mymodule.array())

just one of those things
 
hey mgid,

i suspect that something simple is going on here, and i think jon4747 or myself could help you but it would be difficult without seeing more of your code.

you scare me by saying myfunc(x) instead of
y = myfunc(x), because functions take operands and return a value.

to say myfunc(x) is like saying:
5 '(or whatever the return value is)
that is acceptable in some languages, but i don't know if vb6 is one of them, never tested it.

if i have a return value i don't need, i assign it to a dummy variable:
dummy = myfunc(x)

the most important things to know would be the respective locations of the array declaration, the function or sub that uses it, and of the calling procedure of that function or sub.

if specifying a module name before the array name lets your code work, i suspect that it is a problem of scoping of variables and/or procedures.

good luck,

john




 
My bad, I had
newarray() = myfunc(PublicArray()) which didn't work, but
newarray() = myfunc(Mymodule.PublicArray()) did.

george
 
John,

You are right, MyFunc() represents the value. The syntax also permits to use a Function as a Sub. In VB this means dropping the parenthisis OR putting CALL in front of it. VB uses the parens to signal the type of call.

Assigning the value to a dummy creates the trivial problems of having another name to deal with and the ever so slightly less trivial data movement requirements. Using this duality helps keep code efficient and easier to maintain. It's just a little. But gaining just a little all the time means you're always gaining.

The array problem stems from the parens too. Drop them. Reference the array as though it was a regular variable. After all, it is just another variable.

Let's say I want function it to deal with a dynamic array

[tt]
ReDim x(10) ' its publicly dimmed () as string elsewhere
Dim I As Integer

For I = 0 To 10
x(I) = Format(I * 17, "000000000000")
Next

it x ' Ust it as a sub
Debug.Print it(x) ' Use It as a function
[/tt]


The function It looks like this
[tt]

Function it(a() As String) As Integer
Dim x
For Each x In a
Debug.Print x
Next
it = UBound(a)
End Function
[/tt]


Wil Mead
wmead@optonline.net

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top