.NET C# developer here. Trying to figure out MapBasic with the IDE for the first time. It's shall we say...a different experience to C# in Visual studio.

I have a ButtonPad with a button that calls a stored proc in the SQL server db which performs an insert on one selected polygon in my BidBlock layer:

' User Hit Bid Block
if getSelectionInfo(SelCount, SelTableName) then
    if SelCount = 1 then
        'Have one Contract selected
        if selTableName = vwBidBlocks then
            i_blockId = selection.block_id
            if i_blockId = 0 then
                run menu command 304
                note "Please re-select your Bid Block."
        '       set handler WinChangedHandler on
                exit sub
            end if
            'call notyet()
            call BidBlock(i_blockID)
            dialog error handling blah blah

I'm now trying to implement this function to perform on a selection of > 1 polygons in the layer. I want to therefore make an dynamic array of items using reDim(selection)?

& itterate over the ID's of each selected item in the array calling my BidBlock(i_blockId) function. In C# I would use a for each loop here which implements the IEnumberable interface. Is there any way of doing this in MapBasic?


There's no For Each in MapBasic unfortunately. A selection in MapInfo is effectively a table, so what you want to do is loop through the Selection table. The best way to do that is by using Do...Loop and Fetch. Something like this should work:

Dim alBlockID as Alias
Dim selectionName, selTableName as String
Dim selCount, i_blockId as Integer

selectionName = SelectionInfo(SEL_INFO_SELNAME)
selTableName = SelectionInfo(SEL_INFO_TABLENAME)
selCount = SelectionInfo(SEL_INFO_NROWS)

alBlockID = selTableName & ".block_id"

If selCount > 1 and selTableName = vwBidBlocks then
    Fetch first from selectionName    '// fetch the first record
    Do While Not EOT(selectionName)    '// do while not at the end of the selection table
        i_blockId = alBlockID
        If i_blockId = 0 then
            Run Menu Command 304
            Note "Please re-select your Bid Block."
            '       set handler WinChangedHandler on
            Exit Sub
        End If
        'call notyet()
        Call BidBlock(i_blockID)

        Fetch next from selectionName    '// fetch the next record
End If
  • Great thank you, got the code to invoke my BidBlock(i_blockID) method. This calls the stored proc & executes on my first selected record however the selection is then cleared therefore leaving me with no next record in the selected table. Is there a way to keep the selection alive? – Hardgraf Jul 13 '15 at 10:52

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.