1

I am new to mapbasic and would like to have an array variable (sac) in order to access it through the "For variable = 1 To n" function. I would like to create a loop that only takes on the value of a variable (var) in a table and loops through all the values in that Table.

Include "Mapbasic.def"
Declare Sub Main()

Dim sac(100) As Integer
Dim var As Integer

Sub Main()

Sac(1) = 50
Sac (100) = 1200

For sac = 1 to sac(100)

Next

End Sub
1

You'll need to use a temporary 'index' variable to iterate through the position in the array. Something along these lines should work:

Include "Mapbasic.def"
Declare Sub Main()

Dim sac(100) As Integer
Dim var As Integer

Sub Main()
    Sac(1) = 50
    Sac(100) = 1200

    Dim i As Integer
    For i = 1 To UBound(sac)
        var = sac(i)
    Next

End Sub

Edit: try this:

Include "Mapbasic.def" Declare Sub Main()

Dim sac(100) As Integer
Dim var As Integer

Sub Main()
    Dim iNumberRows As Integer
    iNumberRows = TableInfo( "YOUR_TABLE", TAB_INFO_NROWS )
    ReDim sac(iNumberRows)

    Dim i As Integer
    For i = 1 To UBound(sac)
        Fetch Rec i From YOUR_TABLE
        sac(i) = YOUR_TABLE.var
    Next

End Sub
  • Actually, what I meant is that I cannot give a value to var as it is already given (predetermined) from a specific table. At the sametime I would like to loop thru those various var values from the table. I am not sure if creating an array named sac would resolve my issue ? – user3405572 May 7 '14 at 0:56
  • Try my revised code now. – ndawson May 7 '14 at 1:05
  • This might be unsafe - especially if you have deleted records in your table. Fetching a deleted record will raise an error in your application. Just so you know and take your precautions – Peter Horsbøll Møller May 9 '14 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.