Link to home
Start Free TrialLog in
Avatar of cyyam
cyyam

asked on

Array Handling

Hi,

Hi ,
  I just want to ask how to pass an array into a procedure:
 
Dim strText() As String
 
ReDim strText(FIELD_NUM)
For i = 0 To FIELD_NUM - 1
    strText(i) = Text(i).Text  ' Text(i).Text is the text box in the form
Next

loadOptionCatalogs (strText)   <------------Typ mismatched occurs

where

Sub loadOptionCatalogs(strText() As String)
....
....
End sub

Could anyone tell me the correct method and give some explanation ?

Thanks!
Avatar of GERTJAN
GERTJAN

I think it must be :

Sub loadOptionCatalogs(strText As String)


Avatar of Éric Moreau
use
call loadOptionCatalogs (strText)
or
loadOptionCatalogs strText
it should be:

Sub loadOptionCatalogs(strText As Variant)
ASKER CERTIFIED SOLUTION
Avatar of Jacamar
Jacamar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Only Jacamar's is correct.  The others will only handle single values - not an array variable.
your code worked fine in my machine.  

Private sub Command1_Click()
Dim i as long
Dim strText() As String

ReDim strText(FIELD_NUM)
For i = 0 To FIELD_NUM - 1
   strText(i) = Text(i).Text  ' Text(i).Text is the text box in the form
Next

loadOptionCatalogs  strText  ' <------------Typ mismatched occurs
End Sub


Sub loadOptionCatalogs(strText() As String)
....
....
End sub

actually get rid of the brackets around strText in the marked line....

Good Luck!
you only put brackets around a function call, not for a call to a subroutine (i.e. functions that returns nothing)

e.g.

retVal = FunctionName(parameter)

SubRoutName parameter
' or
call SubRoutName(parameter)
It looks as if the overriding concern is the use of the brackets in the called routine's declaration.  Actually calling the routine can be done with or without brackets.

I was surprised to see this, but this code convinced me.  Just drop in in the code window of a new standard exe project:

Private Sub Form_Load()
   Dim astr(2) As String

   astr(0) = "a"
   astr(1) = "b"
   astr(2) = "c"
   MsgBox "Present order is:" & vbCrLf & _
       astr(0) & vbCrLf & astr(1) & vbCrLf & astr(2)
   
   Call SwapAB(astr)     '  <<---- key line of code here
   
   MsgBox "New order is: " & vbCrLf & _
       astr(0) & vbCrLf & astr(1) & vbCrLf & astr(2)
   
End Sub

Private Sub SwapAB(astr() As String)
   Dim stemp As String
   stemp = astr(0)
   astr(0) = astr(2)
   astr(2) = stemp
End Sub

you'll see that you can change that "key line of code" to either use astr() or astr and the code works the same.  I had always believed you had to use the parens in the call and in the routine declararation.  Always happy to learn that ancient suppositions are wrong.  (Maybe this was right back in VB3??? <g>)

QJ

Avatar of cyyam

ASKER

Thanks for all of your responds, and esp. QJohnson and Jacamar.

May I further a little bit in Array handling.
How can I assign an array to another array?

Dim arstr1 () as string
Dim arstr2() as string

Redim arstr2 ( n )
... do operation in arstr2

arstr1 = arstr2  <---- It does not work

How can I assign it without assigning the array elements one by one ?

Thanks!
I pasted your code into VB6 and it worked fine as soon as I change 'n' to a number.  I used 3.  After the assignment statement executes, ubound(astr1) and ubound(astr2) are both 3.
Hi cyyam,
This old question (QID 20552510) needs to be finalized -- accept an answer, split points, or get a refund.  Please see http://www.cityofangels.com/Experts/Closing.htm for information and options.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

-->Split between QJohnson and Jacamar

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

GPrentice00
EE Cleanup Volunteer
sorry - 20 pts, cant be split.

--> accept answer by Jacamar