#If...Then...#Else Directive

KenHigg

Registered User
Local time
Today, 16:57
Joined
Jun 9, 2004
Messages
13,291
Can anyone give me some examples of when they've used this?
 
What do you mean by 'Directive'?..

I know of the If...Else statement
 
KenHigg said:
Can anyone give me some examples of when they've used this?

I use it for debugging.

For example, the random code below, depending on the status of the constant, performs a different operation on an error. If I'm developing I put the constant atop each module and set it to true so that when testing it tells me the error. If it's live then I don't want it to tell the user but, instead, write the error to the table for review.

i.e.

Code:
#Const ERRORS_ON = True

Private Function MadeUpOnTheSpot(ByVal lngUm As Long) As String
 
    On Error Goto Err_MadeUpOnTheSpot

    Select Case lngUm
        Case Is = 0
            MadeUpOnTheSpot = "One"
        Case Is = 1
            MadeUpOnTheSpot = "Two"
        Case Else
            MadeUpOnTheSpot = "Other"
    End Select

Exit_MadeUpOnTheSpot:
    Exit Function

Err_MadeUpOnTheSpot:
    #If ERRORS_ON Then
        MsgBox Err.Description, vbExclamation, "Error #" & Err.Number
    #Else
        Call WriteErrorToTable(Err.Number, "MadeUpOnTheSpot", "basFunctions")
    #End If
    Resume Exit_MadeUpOnTheSpot
End Function
 
SJ McAbney,

Why would you not want the application to notify the user that the application has experienced a runtime error? Granted that I use a custom error routine to give the user a meaningful error message and how to contact the programmer for help and the information they need to provide [although it is also stored in an errors tracking table]. I am just curious as to why you would not want the user to know that something unexpected has happened and that the application might not be performing as expected because of the runtime error.
 
So it only kicks back compile errors?
 
I have used it to conditionally include Debugging code during development.
 
I forgot Ken,
I use the compiler directives often to switch between early and late binding. Early binding for development with intellisense and late binding for release for user compatability.
 
Sorry, you lost me there... How would you do this?
 
I just cut this out of one of my modules:
Code:
#If LateBinding Then
    Const adSchemaProviderSpecific = &HFFFFFFFF
    Dim cn As Object
    Set cn = CreateObject("ADODB.Connection")
    Dim rs As Object
    Set rs = CreateObject("ADODB.Recordset")
    Dim fld As Object
#Else
    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim fld As ADODB.Field
#End If
 
Hum... Thanks RG, I'll chew on that a bit more tomorrow.
 
I have used it similar to what SJ is refering too... Case statements.
Either way on the error coding, my mentor at work would rather write to a text file for later review instead of relying on users infor. Simple traps for myself for now.

Late binding... isn't there an issue regarding supporting late binding or am I thinking of .Net?
 
Code:
Option Explicit
Option Compare Text

[color=green]'   Change this line to something else for later versions.[/color]
#Const conAccessVersion = "Access97"


[color=green]'   Comment out some code so the compiler doesn't laugh.[/color]
#If False Then
    Option Non Explicit
    Option Compare Widgets

    This code = ful of erors, full of erors, ful of erors.
    Bugger... this code is not good.
    Come to think of it I actually like writing code like this.
#End If


Sub fred()

    MsgBox Replace("Fred *", "*", "who?")

End Sub


#If conAccessVersion = "Access97" Then
    [color=green]'   Access97 does not have a Replace function.[/color]
    Function Replace(ByVal strThisString As String, _
                     ByVal strSearchValue As String, _
                     ByVal strReplacement As Variant, _
            Optional ByVal vntUnknown1 As Variant, _
            Optional ByVal vntUnknown2 As Variant, _
            Optional ByVal vntCompare As Variant = vbTextCompare) As String
    
        Dim lngI         As Long
        Dim strLeftTemp  As String
        Dim strRightTemp As String
        
LookForNext:
        lngI = InStr(1, strThisString, strSearchValue, vntCompare)
    
        If lngI <> 0 Then
           strLeftTemp = Left(strThisString, lngI - 1)
           strRightTemp = Right(strThisString, Len(strThisString) - lngI - Len(strSearchValue) + 1)
           strThisString = strLeftTemp & strReplacement & strRightTemp
           GoTo LookForNext
        End If
        
        Replace = strThisString
    
    End Function
    
#Else
    [color=green]'   Use the later version.[/color]
#End If
 
ghudson said:
SJ McAbney,

Why would you not want the application to notify the user that the application has experienced a runtime error?

I do notify the user but with something friendlier than an obscure Access error message. That was just an example out of my head.
 

Users who are viewing this thread

Back
Top Bottom