views:

98

answers:

3

How do I use Conditional("Condition1") attribute where "Condition1" is an environment variable. The help says it can be done but does not say how ?

+1  A: 

You don' need to do anything more than create the envionment variable (e.g. with a value of 1) and then reference it by name in the ConditionalAttribute. The varable will need to be created in the "Environment Variables" dialog accessible through the advanced tab of the System Properties dialog; i.e. you can't just open a command prompt and set the variable there since that value will only exist within that prompt and be lost when the prompt is closed.

Daniel Renshaw
+5  A: 

I believe this is what you are looking for http://yogesh.jagotagroup.com/blog/post/2008/03/01/CSharp-Conditional-attribute.aspx

The word 'environment variable' refers to VS environment, not the OS or any other. You can simply tag your classes or methods with DEBUG or any custom variable; then, these classes and methods still exist when compiled, but when IL code is generated, they are simply skipped - in case that variable is not defined (like the example says).

Hope this helps.

Pz, the TaskConnect developer

Pz
You can add additional compilation symbols under the properties editor for your project (double click the Properties node in the project) and then reference those within the ConditionalAttribute
Will
+2  A: 

Not sure what Conditional(string condition) does, but you can get your environment variables using Environment.ExpandEnvironmentVariables.


If you're talking about the ConditionalAttribute, you're out of luck. This attribute is evaluated at COMPILE TIME, on your development machine (or on the build server). It has no effect at RUNTIME, where you would want to do something different depending on the user's machine state (as indicated by an environment variable).

You can use the previously mentioned method of the Environment class to determine, within the body of a method, whether or not you should continue execution. That is probably your best bet.

Will
Yes COMPILE TIME was what i was after.
Kaya
Awesome @kaya. I love COMPILE TIME. You can set up environment variables within the project properties, which should do what you need (unless you are trying to do things differently on different machines).
Will