|
|
Title | Use the Conditional attribute to make a method non-callable in VB.NET |
Description | This example shows how to use the Conditional attribute to make a method non-callable in VB.NET. |
Keywords | ReDim, array |
Categories | Miscellany, VB.NET |
|
|
The Conditional attribute makes a method callable depending on whether a compile-time constant is defined. The following code makes the Test subroutine callable if either the DIAG or TEST constant is defined.
|
|
<Conditional("DIAG"), Conditional("TEST")> _
Private Sub Test()
MessageBox.Show("Test", "Test", MessageBoxButtons.OK)
End Sub
|
|
This would make the most sense when the arrays are closely related, perhaps even if they always have the same bounds. It could be confusing if the are unrelated.
|
|
To define a constant, open the Project menu, select Properties, click the Configuration Properties tab, click the Build item, and in the "Custom constants" box enter "DIAG=True".
If a method is not callable, Visual Basic still generates code for it and still checks the calling routine's parameters against those required by the method, but it does not perform the call at run time.
You can use #if to exclude code from compilation but if you exclude a method in this way, code that calls the method will generate an error. Using Conditional allows you to hide a method safely.
|
|
|
|
|
|