Scope of Pragma Clarification?

I know pragmas only affect the method they are in, but what about within the method?

A) Are they only effective from the point in the method they were set, or for the whole method (I have always assumed from that point onward but he docs don’t explicitly say that last I looked.)

B) If defined in an if block like this:

If usePragmas Then
  #Pragma BackgroundTasks False
  #Pragma BoundsChecking False
End if

are they:

  1. only effective in that block
  2. only if set in block, they are effective from that point onward
  3. the if block ignored and they are set anyway

I always assumed 2 but again I just realized that might not be the case


They are effective from that point onward within the method unless contradicted by a subsequent pragma.

Sub Foo()
#pragma BackgroundTasks Off
' background tasks is now off
' do stuff
#pragma BackgroundTasks On
' background tasks is now on
' do more stuff
End Sub

There is another consideration in the code I posted.

What made we wonder about scope is that unlike #if#endif blocks, plain If…end statements are executed at run time, but Pragmas are a compile time thing.

Thinking about that made we wonder what happens with Pragmas in IF…END blocks rather than being in #IF#ENDIF blocks

If the complier is smart enough it might work as one intuitively might expect, but it just as easily might not.

BTW In my code use usePragmas is a variable passed into the method


I believe program logic (If…End If) has no effect on compiler directives.

As an experiment I made a method that produces an “unused method parameter” compile warning and put #pragma Unused in a logic block that will never execute. It still made the warning go away:

Sub Foo(i As Integer)
  If False Then
    #pragma Unused i
  End If
End Sub

The Pragma will be evaluated at compile time as usual. The compiler cannot know which way the IF will be evaluated at run time.

Inside an #IF block would be different.