Our full technical support staff does not monitor this forum. If you need assistance from a member of our staff, please submit your question from the Ask a Question page.


Log in or register to post/reply in the forum.

Initialize Variables with a for loop


rwc Jun 26, 2017 10:18 PM

Hi all:

I'm putting together a program to read a number of instruments on a weather buoy.  The current profiler I'm using has several different depth bins that I need to keep track of seperately.  I can of course hard code it for now, but it would nice to have something more general in case I change the number of bins.   There are 40 bins in the current set up,  and I tried the following:

Dim i
For i = 0 to 39 Step 1
  Public "Bin" & i & "_spd" As Long
  Public "Bin" & i & "_dir" As Long
Next i

....that does not compile ("Invalid Variable declaration &.").  It seems to be concatenated correctly to me,  I am wondering if there's something additional I need to do for the concatenated string to be recognized as the VarName.

Thanks, Rob


JDavis Jun 26, 2017 10:40 PM

A variable name needs to be constant.

You will probably just needs to declare arrays like:

     Public Spd(40) as Long

     Public Dir(40) as Long

Note that arrays are 1 based in CRBasic.


smh Jun 27, 2017 08:42 AM

You could have a constant defined at the top of your program - easer to maintain if/when you alter the number of bins

Const NUM_BINS=40

Public Spd(NUM_BINS)

Public Dir(NUM_BINS)

DataTable (BinData,1,100)

Sample(NUM_BINS,Spd,FP2)

Sample(NUM_BINS,Dir,FP2)

EndTable

'In Program

For i = 1 to NUM_BINS

'populate Spd(i)

'Populate Dir(i)

Next

Log in or register to post/reply in the forum.