Question Asker Question Asker - 1 year ago 53 Question

Debugging with my own custom Class Libraries

I want my class library conditionally compiled so that it is in debugging mode when my project is, and is not when my project isn't.

For example, I have this Module in my class library:

Module MyDebug

Sub print(ByVal msg As String)
Debug.Print(Now.ToString("yyyy-MM-dd HH:mm:ss.fff") & " " & msg)
End Sub

Sub debugEnd(Byval bool As Boolean)

End Sub
End Module

When I debug my project which references this library it run any of these when they are called.

I've tried searching online, but I haven't found anything that helps since I can only find things to do with debugging the actually class library, while I only want it to send these conditionally compiled statements while I'm debugging my project.

However, by experimenting around a bit, I've found that if in the class library I go to:

'My Project' -> the 'Compile' tab -> 'Advanced Compile Options'

and then tick 'Define DEBUG constant' (and then build the library), the project does run the debug statements when called.

However, I'm not entirely sure of the behaviour of 'Define DEBUG constant' in the class library. Does it define DEBUG if and only if my project is in debug mode?

If not, then is there a simple way to acheive what I aim to do? (I don't want to have to tick/untick the checkbox in the class library each time I switch between debug and release in my project, and my class library is referenced by more than one project, anyway)

Note that in the project I'm referencing the
in the
Bin -> Release
folder of my class library which I hope is the right way to reference it.

In addition, I would like to ask about how VB acheives this with the
class, because it is also imported with a reference like any other class library, and works in the way I would like the above to — surely I could do the same?

Answer Source

If you keep the class library project in the same solution as your main project it will use the same configuration (Debug or Release) as every other project in that solution. This means that you won't have to manually check/uncheck Define DEBUG constant as it will not be defined anyway if you have set the configuration to Release.

Even if your projects are not in the same folder you may still add your class library project to your solution. Here's how to do it:

  1. Right-click your solution in the Solution Explorer and goto Add > Existing Project.

  2. In the file browsing dialog that opens, locate the .vbproj-file of your class library and click OK.

  3. Now when you change compilation configuration it should be reflected over the entire solution and your class library shall only have its DEBUG constant defined if you set the configuration to Debug.

If you cannot see your solution in the Solution Explorer:

  1. In Visual Studio, goto Tools > Options > Projects and Solutions > General.

  2. Check Always show solution and press OK.

For the above to work you must also change the way you reference your class library. The way you are currently doing it is correct, but it won't work in this case as then you'll only be referencing the Release version of your dll.

  1. Start by removing your current reference to your class library.

  2. Open the Add Reference dialog and go to the Projects tab.

  3. Select your class library's project and press OK.

This should now reference the class library's output from the solution's current compilation configuration.