Pragma Unused .. working or not?

I have the below code

Method test(t as string)
#Pragma unused t
msgbox t
end

test("hello world")

-> this shows a message box Hello World.

How can this happen when the pragma indicated t isn’t used?

This is not how it works

[quote]Controls whether Analyze Project will test for the passed unused variable. VariableName can be a local variable, method parameter, or event parameter. You must pass the variable or parameter you do not want to test. The pragma can be used only after the variable has been declared. Also, you cannot pass a list of variables. Use a separate #pragma statement for each variable. You can also turn off checking within the Issues pane by clicking the Type Filter button In the Issues toolbar and deselecting the checks for unknown local variable, method parameter, or event parameter.
#Pragma Unused Column[/quote]

http://documentation.xojo.com/index.php/Pragma

If you simply want to prevent execution of a code block, you could use something like if 1 = 2 then / end if

Or…

if false then
    // code
end if

Later change it to True to execute the code.

If you want the code excluded from the app entirely…

#if false
  // code
#endif

BTW, the first example is also a convenient way to limit scope of a variable. Consider:

if true then
  dim t as string = "something"
  MsgBox t
end if

// t no longer exists

#Pragma Unused merely suppresses a compiler warning that a variable is unused. If you end up using the variable then the pragma has no effect because there’s no warning.

What is it you expected to happen when “MsgBox t” was executed?

Ok, I wasn’t very clear on my question. :slight_smile:

The code above was just to make an example clear that when you use Pragma unused it still works.
Mostly I use Analyse Project and use Pragma unused for variables it says aren’t used.
By mistake I used Pragma Unused for a variable that was using and to my surprise it still worked. I always thought when you do a Pragma Unused, it will ignore that variable. Apparently this isn’t the case.
So another question raises: why would I use Pragme Unused when it isn’t working? :slight_smile:

I would expect a crash or an exception would be thrown.
Also I thought the compiler would allocate no memory for the variable.

It’s working, it just doesn’t let you know when you don’t need it anymore :slight_smile:

Oh, I see what you’re thinking. That it effectively removes that variable from the code. But then it couldn’t even compile “MsgBox t” because it doesn’t know what t is.

If you have a variable in scope and don’t use it then you get a compiler warning. All Pragma Unused does is hide that compiler warning. Otherwise your code compiles and runs the same.