Results 1 to 11 of 11

Thread: #ifdef #if #else and classes.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Location
    Norway
    Posts
    112

    Question

    I have a module that is used in several projects. If a certain class is present I want to declare this class inside the module. Is there a way to use the #ifdef to do this?
    Code:
    #If clsMyClass-Exist
      Dim clsMyClass As New clsMyClass
    #End If
    I will be using that further down in the code as well to use the class if it is available.

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    something like this?
    Code:
    If not clsMyClass is nothing then
       Dim clsMyClass As New clsMyClass
    End If
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Location
    Norway
    Posts
    112
    I was thinking to have it globally inside the class so i dont have to pass it to every function.

    Code:
    Private MyClass As New clsMyClass
    Dont know if one can use normal if else outside a function or sub.

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    No If statements outside functions! Just declarations.
    Well now i understand your point, you want to check if a class module is in the project, well no that won't get compiled, sorry
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5
    New Member
    Join Date
    Oct 2000
    Posts
    9
    By definition, what you want to do is conditional compilation. Therefore, you cannot check against a global variable other than what is stored in the conditional compilation arguments.

    What you need to do is put something like blnGoodProject = 1 as a conditional compilation argument. You could then have:

    #If blnGoodProject = 1 Then
    Set objItme = New clsItem
    #End If

    Unfortunately, since conditional compilation only allows boolean values (whatever evaluates to true and false), you can't really have a Select Case statement either in your code. Not that there is a #Select Case... statement anyway.

    BTW, please do not do this:

    #If Not blnGoodProject = 1

    Not is a bitwise operator, this will not evaluate properly. Do a blnGoodProject <> 1 instead.


  6. #6
    Addicted Member
    Join Date
    May 2000
    Posts
    247
    What is the difference when you place a # sign infront of "IF"?
    Mako Shark
    Great White

  7. #7
    New Member
    Join Date
    Oct 2000
    Posts
    9
    Shark,

    That means it's conditional compilation. If the #If statement does not evaluate to true, whatever code is in there will not be compiled either in debug mode or to the compiled component.

    Therefore, you can make a reference to a class that does not exist in the project, as long as the IF...Then statement does not evaluate.

  8. #8
    Addicted Member
    Join Date
    May 2000
    Posts
    247

    Thanx for the definition.

    Can you give me an example of both "If" and "#If"? I like to know when to use which?

    Thanks

    [Edited by Shark on 10-10-2000 at 02:06 PM]
    Mako Shark
    Great White

  9. #9

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Location
    Norway
    Posts
    112
    Here is an example
    Code:
    Private Const SWpresent = 1 '1=true 0=false
    #If SWpresent = 0 Then
      Private Const MyNumb = 1000
    #Else
      Private Const MyNumb = 99
    #End If
    
    Sub Test()
      MsgBox MyNumb
    End Sub
    I actually get the inverse of what the If-line says.
    Output here is 1000. If i change "SWpresent = 0" to "SWpresent = 1" i get 99. Weird.

  10. #10
    Addicted Member
    Join Date
    May 2000
    Posts
    247
    Thanks Thomas, that make sense. So when you place a "#" sign infront of an If, you can use it outside of a procedure?
    Mako Shark
    Great White

  11. #11

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Location
    Norway
    Posts
    112
    Yes you can in a way do this. But it tells the compiler what to compile and what not to compile. Normally I would get an error if I declared a class that did not exist. With the #if #else I can tell it only to declare it if it exist. In my case I just declare a Constant saying if I have the class or not. Without the #if #else, you would get an error when pressing F5 to run, before the execution actually begin. The compiler would say "That class does not exist" and wont run the program.

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