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

dynamic array allocation? 1

Status
Not open for further replies.

drewdaman

Programmer
Aug 5, 2003
302
CA
hi,
i'm used to c++! sorry.. i have a very basic question.

in c++, you can dynamically allocate arrays. ie:

int a;
a=some calculation, say a has 100

char * a= new char [a] //this is very very legal and done all the time

in vba, however, i'm having some trouble:

Code:
Sub test()
    Dim numwc As Long
    Dim lowlim As Long
    Dim uplim As Long
    Dim inc As Long
    
    lowlim = 1000
    uplim = 5000
    inc = 500
    
    numwc = ((uplim - lowlim) / inc) + 1
    Dim wcs(numwc) As Long
    
End Sub

this code crashes on the line:
Dim wcs(numwc) As Long

anyone know if its possible to allocate memory dynamically, or is there some other way to do it? syntax etc?

please note, that the abvoe code is not my real code.. i'm just playing around with arrays right now to see what vba supports and does not support! (my code makes more sense :))

thanks!
 
Have a look at ReDim:
Dim wcs() As Long
...
ReDim wcs(numwc)
...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
thanks! that worked.

one more related question..
how do you pass an array to a function/sub?

Code:
Sub t1()
    Dim a(4) As Long
    t2 (a) 'crashes!!
    s = CStr(a(2))
    MsgBox (s)
End Sub

Sub t2(ByRef b() As Long) 'is this correct?
    b(2) = 6
End Sub

i guess i'm trying to use c++ conventions in a language i know nothing about!
 
Dim a(4) As Long, s
t2 a 'don't crashes!!


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
well. if i remove the () around "a" then it doesn't crash as you pointed out.. but if i want to pass multiple parameters, how can i do that?
 
Either:
t2 a, arg2, arg3
or:
Call t2(a, arg2, arg3)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top