Click here to Skip to main content
Licence 
First Posted 18 Jan 2006
Views 17,853
Bookmarked 9 times

Conditional Methods using Conditional Attribute

Conditional Methods using Conditional Attribute
2 votes, 20.0%
1
2 votes, 20.0%
2
1 vote, 10.0%
3
1 vote, 10.0%
4
4 votes, 40.0%
5
2.50/5 - 10 votes
μ 2.50, σa 2.98 [?]

Conditional Methods using Conditional Attribute

Conditional Attribute is defined in System.Diagnostics namespace.

When a conditional method is called its call is determined by the presence or absence of the preprocessor symbol used with the conditional attribute. If this preprocessor symbol is defined then the mehotd call is included else omitted.

Using conditional attibute is an alternative to calling methods in #if & #endif preprocessors.

A method can have multiple conditional attributes. The call to such a method is made if at least one of the conditional symbols is defined.

A conditional method has a few constraints:

  • A conditional method must be a method in a class or struct declaration and should not have any return type ... i.e. the return type should be "void".
  • The conditional method must be a method in a class or struct declaration a compile-time error occurs if the Conditional attribute is specified on a method in an interface declaration.
  • Conditional methods cannot be marked with override keyword.
  • Conditional methods can be virtual and the methods which override such a conditional method implicitly becomes Conditional
  • Methods defined in an interface cannot be marked as conditional.

Example:

Steps :

1. Create a new Console Application and define 2 classes MyClass and MyClient.

2. In MyClass define two as shown below.

<FONT color=#0000ff><FONT face=Arial>class MyClass

<FONT size=2>{</FONT></FONT></FONT>
<BLOCKQUOTE>
<FONT face=Arial color=#0000ff size=2>[Conditional("CON_Attrib1")]</FONT>

<FONT face=Arial color=#0000ff size=2>public void MyFun1(string name)</FONT>

<FONT face=Arial color=#0000ff size=2>{</FONT>

<FONT face=Arial color=#0000ff size=2>    Console.WriteLine(name);</FONT>

<FONT face=Arial color=#0000ff size=2>}</FONT>

<FONT face=Arial color=#0000ff size=2>[Conditional("CON_Attrib1"),Conditional("CON_Attrib2")]</FONT>

<FONT face=Arial color=#0000ff size=2>public virtual void MyFun2(string name)</FONT>

<FONT face=Arial color=#0000ff size=2>{</FONT>

<FONT face=Arial color=#0000ff size=2>Console.WriteLine(name);</FONT>

<FONT face=Arial color=#0000ff size=2>}</FONT>
</BLOCKQUOTE><P>
<FONT face=Arial color=#0000ff size=2>}</FONT></P>
3. In MyClient write the preprocessor directives as shown . (They should be the first two lines in the file... above using System ... if not it will not compile)

#define CON_Attrib1

#define CON_Attrib2

4. Call the My Class methods from the main as shown

<FONT color=#0000ff><FONT face=Arial>public class MyClient

<FONT size=2>{</FONT></FONT></FONT>
<BLOCKQUOTE><FONT face=Arial color=#0000ff size=2>public MyClient()</FONT>

<FONT face=Arial color=#0000ff size=2>{</FONT><FONT face=Arial color=#0000ff size=2>}</FONT>

<FONT face=Arial color=#0000ff size=2>public static void Main() </FONT>

<FONT face=Arial color=#0000ff size=2>{</FONT>
 
<FONT face=Arial color=#0000ff size=2>   MultiFile1.MyClass oMyClass = new MyClass();</FONT> 
<FONT face=Arial color=#0000ff size=2>   oMyClass.MyFun1("Namratha");</FONT>  
<FONT face=Arial color=#0000ff size=2>   oMyClass.MyFun2("Nasha");</FONT>  
<FONT face=Arial color=#0000ff size=2>   Console.ReadLine();</FONT>

<FONT face=Arial color=#0000ff size=2>}</FONT>
</BLOCKQUOTE><P>
<FONT face=Arial color=#0000ff size=2>}</FONT></P>

4. Build the solution and run . You will see both "Namratha" & "Nasha" ... i.e both the functions were called.

5. Now change the #define CON_Attrib1 to #undef CON_Attrib1. ... i.e. undefining the attribute.

6. Build the solution and run . You will see only "Nasha" ... i.e MyFun1 was not called as the preprocosser directive "CON_Attrib1" is not defined. But MyFun2 is called coz the other preprocessor directive "CON_Attrib2" is defined. When there are moer than one directives it an OR condition hence if any of the preprocessor directive is define the function will be called.

7. MyFun2 is marked as virtual hence if another function overrides its then is becomes conditional implicitly.

Let's keep things Simple .....

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Namratha Shah

Architect

United States United States

Member
Namratha Shah a.k.a. Nasha is from orginally from Bombay, India but currently residing NJ, USA. She has to her credit, a Bachelor’s Degree in Microbiology and Biotechnology and a Master's in Computer and Software Applications (1999-2001) from Somaiya College Bombay. She started her career with C and C++ and then moved on to Microsoft Technologies. She has over 7.5 years experience in software architecture, design and development. She is a Certified Scrum Master and a member of the CORE .NET Architecture team. She has been Awarded with Microsoft’s Prestigious Most Valuable Professional (MVP) twice consecutively in years 2005 and 2006 in Visual C#.NET for her outstanding contributions to the .NET community.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionControlling #DEFINE values from an external property file PinmemberMember 47134440:07 28 Oct '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120209.1 | Last Updated 18 Jan 2006
Article Copyright 2006 by Namratha Shah
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid