2016-08-16 19 views
1

Debug類很有用,因爲它允許調試代碼在不需要在發佈時刪除的情況下編寫,因爲涉及該類的任何調用都不會被編譯。對調用函數的任何調用只能在調試中編譯

如何使我自己的自定義模塊或類相同?舉例來說,我已經寫了這一點:

Module MyDebug 

    Sub print(ByVal msg As String) 

     Debug.Print(Now.ToString("yyyy-MM-dd HH:mm:ss.fff") & " " & msg) 

    End Sub 
End Module 

我能找到的唯一解決方案是使用宏,但似乎是荒謬的,因爲我寫的東西,如:

#If DEBUG 
MyDebug.Print("...") 
#endif 

在每點我需要在代碼中進行調試,與單線程相比,如果我要使用Debug.Print("...")

+0

我發現,調試並不一定與類庫工作,所以相關:http://stackoverflow.com/questions/38983688/debugging-with-my-自定義類庫 – Shuri2060

回答

2

您想用<Conditional>屬性標記您的方法。

Module MyDebug 

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

MSDN ConditionalAttribute

Reference Source for Debug class that uses the attribute

+0

謝謝 - 我不應該錯過這個。 – Shuri2060

+0

不用擔心。許多人甚至沒有意識到它存在。它有時會非常方便。 – TyCobb

+0

是否可以標記整個模塊/類? – Shuri2060

相關問題