Results 1 to 4 of 4

Thread: Conditional Compilation for Declare Statements

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2014
    Posts
    6

    Conditional Compilation for Declare Statements

    I have several User Defined Types (UDT) of different DEVMODE structures that I would like to declare a variable with conditionally based on passing a variable argument to a function. My test code looks like:
    Code:
    Function TestConditionalCompile(DevModeType As Long) As Boolean
    
    #If DevModeType = 1 Then
        Dim DM As DEVMODEPS500
    #ElseIf DevModeType = 2 Then
        Dim DM As DEVMODEPS
    #ElseIf DevModeType = 3 Then
        Dim DM As DEVMODEUNI500
    #ElseIf DevModeType = 4 Then
        Dim DM As DEVMODEUNI
    #Else
        Dim DM As DEVMODE
    #End If
    'execution code goes here
    End Function
    However, when I tested this code, no matter what DevModeType value I send to the function it always dimensions DM as the first UDT (DEVMODEPS500).

    My question: Is it possible to dimension a variable conditionally based on a passed variable? If yes, how would I modify my code to achieve this?

    Note: I cannot use the variant data type as it will not except a UDT.

    Thanks,

    Tom

  2. #2
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,372

    Re: Conditional Compilation for Declare Statements

    your code is mixing up conditional compilation based on compiler constants with function parameters which are only available and evaluated at run time.
    what api are you dealing with? if you declare the parameter that will receive DM 'as any' you should be able to use a variant.

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2014
    Posts
    6

    Re: Conditional Compilation for Declare Statements

    I infer from your answer that you can only conditionally compile with constants (which is what I thought was true). Since I don't know which devmode structure I need until runtime this method will not work for me. To answer your question I am using CopyMemory (which requires a devmode structure) to set extended printer features. The required devmode changes with each printer. I can make it work with multiple select cases, but the conditional dim would have made it easier. Here is a snippet of the code FYI.

    Code:
    'code example of seting reverse print feature - it has to be duplicated for multiple features
    Select Case lDevmodeType
      Case DEVMODE_BASIC
        Exit Sub  'Can't set reverse print in the basic devmode
      Case DEVMODE_UNIDRVEXTRA
        DMU.bReversePrint = 1
      Case DEVMODE_UNIDRVEXTRA500
        DMU.bReversePrint = 1
      Case DEVMODE_PSDRVEXTRA
        DMPS.bReversePrint = 1
      Case DEVMODE_PSDRVEXTRA500
        DMPS500.bReversePrint = 1
    End Select
    
    'after all printer features are set then write to proper devmode
    Select Case lDevmodeType
       Case DEVMODE_BASIC
        Call CopyMemory(yDevmodeArr(1), DM, Len(DM))
      Case DEVMODE_UNIDRVEXTRA
        Call CopyMemory(yDevmodeArr(1), DMUNI, Len(DMUNI))
      Case DEVMODE_UNIDRVEXTRA500
        Call CopyMemory(yDevmodeArr(1), DMUNI500, Len(DMUNI500))
      Case DEVMODE_PSDRVEXTRA
        Call CopyMemory(yDevmodeArr(1), DMPS, Len(DMPS))
      Case DEVMODE_PSDRVEXTRA500
        Call CopyMemory(yDevmodeArr(1), DMPS500, Len(DMPS500))
    End Select
    Regards,

    Tom

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2014
    Posts
    6

    Re: Conditional Compilation for Declare Statements

    I infer from your answer that you can only conditionally compile with constants (which is what I thought was true). Since I don't know which devmode structure I need until runtime this method will not work for me. To answer your question I am using CopyMemory (which requires a devmode structure) to set extended printer features. The required devmode changes with each printer. I can make it work with multiple select cases, but the conditional dim would have made it easier. Here is a snippet of the code FYI.

    Code:
    'code example of seting reverse print feature - it has to be duplicated for multiple features
    Select Case lDevmodeType
      Case DEVMODE_BASIC
        Exit Sub  'Can't set reverse print in the basic devmode
      Case DEVMODE_UNIDRVEXTRA
        DMU.bReversePrint = 1
      Case DEVMODE_UNIDRVEXTRA500
        DMU.bReversePrint = 1
      Case DEVMODE_PSDRVEXTRA
        DMPS.bReversePrint = 1
      Case DEVMODE_PSDRVEXTRA500
        DMPS500.bReversePrint = 1
    End Select
    
    'after all printer features are set then write to proper devmode
    Select Case lDevmodeType
       Case DEVMODE_BASIC
        Call CopyMemory(yDevmodeArr(1), DM, Len(DM))
      Case DEVMODE_UNIDRVEXTRA
        Call CopyMemory(yDevmodeArr(1), DMUNI, Len(DMUNI))
      Case DEVMODE_UNIDRVEXTRA500
        Call CopyMemory(yDevmodeArr(1), DMUNI500, Len(DMUNI500))
      Case DEVMODE_PSDRVEXTRA
        Call CopyMemory(yDevmodeArr(1), DMPS, Len(DMPS))
      Case DEVMODE_PSDRVEXTRA500
        Call CopyMemory(yDevmodeArr(1), DMPS500, Len(DMPS500))
    End Select
    Regards,

    Tom

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