-
Aug 26th, 2005, 03:28 AM
#1
Thread Starter
Fanatic Member
Prevent compilation
Why would you want to do this?
I had to write and test some code to hook up to a piece of external equipment.
To test the code when the equipment was not there, I set a variable TESTING which generated fake replies from the equipment so that the rest of the program could be tested.
The problem? What if some idiot (i.e. me) forgot to reset the TESTING variable, compiled the program and sent it to the customer. Disaster!
This code prevents compilation when TESTING is true.
Another approach would have been to see if we were in the IDE and generate fake data when we were, but that would not have worked here as we sometimes needed to debug in the IDE with the equipment present.
VB Code:
Option Explicit
Public TESTING As Boolean
#Const TESTING = True 'Set true or false as required
Sub TestingCheck()
'Dummy procedure which is never called.
#If TESTING Then
Will not compile if TESTING is true
'The above line appears red in the VB6 code editor.
'That's expected because we are forcing a compile error. Don't edit it!
#End If
End Sub
Public Sub Main()
'Set a public variable so we can check it from
'anywhere in the program. (The compiler constant
'with the same name applies ONLY in the module
'in which it is declared.
#If TESTING Then
TESTING = True
#End If
'Example of use >>>>>>>>>>>>>>>>>>>
'Dim DataFromSpacecraft As String
'If TESTING Then
'
' 'Supply dummy data for testing the application
' DataFromSpacecraft = "Hello earth!"
'
'Else
'
' 'Get real data
' DataFromSpacecraft = GetDataFromSpacecraft()
'
'End If
'Example of use >>>>>>>>>>>>>>>>>>>
End Sub
Last edited by BrianHawley; Aug 26th, 2005 at 03:35 AM.
Reason: Added explanation
Brian
(Fighting with the RightToLeft bugs in VS 2005)
-
Aug 26th, 2005, 03:38 AM
#2
Re: Prevent compilation
Why would it not compile if TESTING is true?
-
Aug 26th, 2005, 03:42 AM
#3
Thread Starter
Fanatic Member
Re: Prevent compilation
The code...
VB Code:
#If TESTING Then
Will not compile if TESTING is true
#End If
...does the work.
If TESTING is true the compiler tries to compile "Will not compile if TESTING is true" and falls over because it is an illegal line. If TESTING is false, the complier just misses it out and does not try to compile it.
Brian
(Fighting with the RightToLeft bugs in VS 2005)
-
Aug 26th, 2005, 03:52 AM
#4
Re: Prevent compilation
Sorry, didn't try it before I asked.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|