Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleUse the Conditional attribute to make a method non-callable in VB.NET
DescriptionThis example shows how to use the Conditional attribute to make a method non-callable in VB.NET.
KeywordsReDim, array
CategoriesMiscellany, 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.

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated