Results 1 to 4 of 4

Thread: Prevent compilation

  1. #1

    Thread Starter
    Fanatic Member BrianHawley's Avatar
    Join Date
    Aug 2001
    Location
    Saudi Arabia
    Posts
    796

    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:
    1. Option Explicit
    2. Public TESTING As Boolean
    3. #Const TESTING = True 'Set true or false as required
    4.  
    5. Sub TestingCheck()
    6.  
    7.   'Dummy procedure which is never called.
    8.  
    9.   #If TESTING Then
    10.     Will not compile if TESTING is true
    11.     'The above line appears red in the VB6 code editor.
    12.     'That's expected because we are forcing a compile error. Don't edit it!
    13.   #End If
    14. End Sub
    15.  
    16. Public Sub Main()
    17.  
    18.   'Set a public variable so we can check it from
    19.   'anywhere in the program. (The compiler constant
    20.   'with the same name applies ONLY in the module
    21.   'in which it is declared.
    22.   #If TESTING Then
    23.     TESTING = True
    24.   #End If
    25.  
    26.   'Example of use >>>>>>>>>>>>>>>>>>>
    27.   'Dim DataFromSpacecraft As String
    28.   'If TESTING Then
    29.   '
    30.   '  'Supply dummy data for testing the application
    31.   '  DataFromSpacecraft = "Hello earth!"
    32.   '  
    33.   'Else
    34.   '
    35.   '  'Get real data
    36.   '  DataFromSpacecraft = GetDataFromSpacecraft()
    37.   '  
    38.   'End If
    39.   'Example of use >>>>>>>>>>>>>>>>>>>
    40.  
    41.  
    42. 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)

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    Re: Prevent compilation

    Why would it not compile if TESTING is true?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3

    Thread Starter
    Fanatic Member BrianHawley's Avatar
    Join Date
    Aug 2001
    Location
    Saudi Arabia
    Posts
    796

    Re: Prevent compilation

    The code...

    VB Code:
    1. #If TESTING Then
    2.     Will not compile if TESTING is true
    3.   #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)

  4. #4
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    Re: Prevent compilation

    Sorry, didn't try it before I asked.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width