0

The following:

#If False Then
    Public Type Long
        LoPart As Integer
        HiPart As Integer
    End Type
#End If

Throws a compiler error "Expected: Identifier" when run on my VBA7 32-bit office (Excel). This is because Long is a protected type. However, it was my understanding that the compiler should not see what's in the block (since the condition is always false).

Why do I still get the error?

I can still write and run code using Long variables - my type doesn't show up in intellisense. But the line

Public Type Long

remains red in the editor indicating some problem. Is it something to worry about?


NB, I'm trying to write a replacement for LongPtr for pre VBA7 systems where it didn't exist - in the off-chance that someone manages to run VBA6 in 64 bit office - I bet there's a way...

Greedo
  • 4,967
  • 2
  • 30
  • 78
  • @RobertBaron No this isn't a duplicate, that question is about setting something based on a non-constant `#IF condition` - a condition that can't be evaluated at compile time because it relies on a function that doesn't exist yet. My question uses `False` at compile time which is valid (according to the [language reference](https://docs.microsoft.com/en-us/openspecs/microsoft_general_purpose_programming_languages/ms-vbal/efd3d84d-0d78-4456-882e-8422cf2938a5)). I'm asking about the validity of what's inside the If block; namely a type that's illegal only under some versions of VBA/Excel – Greedo Jun 30 '19 at 18:32
  • You tried to use `Const` and you got the same error? – RobertBaron Jun 30 '19 at 18:44
  • @RobertBaron yep :/ - the 2 statements should be entirely equivalent anyway, since a `Boolean` is a form of *Numeric literal* which the language specification I linked expressly permits inside Conditional Compilation blocks – Greedo Jun 30 '19 at 18:47
  • I guess then that the compiler compiles the contents of both the `#If` and the `#Else`. This is not truly conditional compilation as one would expect. I agree with you. It is a bit surprising! – RobertBaron Jun 30 '19 at 18:57
  • 1
    @RobertBaron It's weirder than that; the automatic compiler used by the IDE flags it with an error when first typed and afterwards highlights it in red regardless of the condition. However re-compiling manually by running code or _Debug_ > _Compile_ will raise an error when the condition is True, but not when it's False (as expected). So only the syntax highlighting appears to be at fault - but I'd like a more thorough understanding in case I use this in real code. – Greedo Jun 30 '19 at 19:04
  • If your application is a critical application, then you might want to maintain two versions of it to be on the safe side. Otherwise, keep it as is, – RobertBaron Jun 30 '19 at 19:11
  • If ***WHAT*** is false in the first line of code? Is this a typo or is that your code? If it's your code, then that's the problem. If it's a typo, [edit] the question so that the problem can be correctly analysed. – Cindy Meister Jul 01 '19 at 11:24
  • @CindyMeister That is always False on purpose. The code in the compilation guard should not be compiled under any circumstances and that's what's confusing me; the IDE is highlighting it in red. In my real code I have a proper condition there, but this is just a min-repro. Note that as in my previous comment, this doesn't seem to lead to any issues unless I set it to True instead – Greedo Jul 01 '19 at 14:34

0 Answers0