468,747 Members | 1,755 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,747 developers. It's quick & easy.

[Conditional] question

Hi there,

I am trying to get some conditional functionality into some of my
apps. I can use the format :

[Conditional("DEBUG")]
private void doStuff()
{
//do something
}

as expected, but I would like to makr it a little more useful in a
multi-machine environment.

I want to make tweaks to an application based up whether it is running
on my development laptop, the stage server or the live server, and
conditionals seem to be the most efficient way forward. However, I am
not sure how to go about defining an environment variable to trigger
the consitional method.

Here's what I *think* I want to do...
**snip unnecessary stuff**

string outputMessage;
string productionServer;

private string statusMessage()
{
outputMessage = "This application is running on " + Environment.HostName + ". ";
productionServer = "This is the production server. ";
isDevBox();
return outputMessage + productionServer;
}

[Conditional( ***HERE IS WHERE I AM COMING UNSTUCK****)]
private void checkIsDevBox()
{
//reset things like connection strings, data table names, development queries, and whatever else I need
productionServer = "This is one of the development of stage servers. ";
}
**snip unnecessary stuff**


It seems that I need to check if Environment.Hostname matches one of
my two development server names, and if so, set a flag that
[Conditional] can work with. It's all very well to know this is what
I need to do, but I have not the first clue about how to go about that
bit.

Can anyone help, please?

Thanks
Marc.
Nov 16 '05 #1
6 1390
Marc,

You aren't going to be able to do this. The reason is that the
Conditional attribute is evaluated at compile-time, not run-time. It is
strictly for pre-processor values, not environment values.

If you want to do something based on the environment, you are going to
have to write code that will detect the environment you are in, and then
make the appropriate calls.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Marc Jennings" <Ma**********@community.nospam> wrote in message
news:p7********************************@4ax.com...
Hi there,

I am trying to get some conditional functionality into some of my
apps. I can use the format :

[Conditional("DEBUG")]
private void doStuff()
{
//do something
}

as expected, but I would like to makr it a little more useful in a
multi-machine environment.

I want to make tweaks to an application based up whether it is running
on my development laptop, the stage server or the live server, and
conditionals seem to be the most efficient way forward. However, I am
not sure how to go about defining an environment variable to trigger
the consitional method.

Here's what I *think* I want to do...
**snip unnecessary stuff**

string outputMessage;
string productionServer;

private string statusMessage()
{
outputMessage = "This application is running on " + Environment.HostName
+ ". ";
productionServer = "This is the production server. ";
isDevBox();
return outputMessage + productionServer;
}

[Conditional( ***HERE IS WHERE I AM COMING UNSTUCK****)]
private void checkIsDevBox()
{
//reset things like connection strings, data table names, development
queries, and whatever else I need
productionServer = "This is one of the development of stage servers.
";
}
**snip unnecessary stuff**


It seems that I need to check if Environment.Hostname matches one of
my two development server names, and if so, set a flag that
[Conditional] can work with. It's all very well to know this is what
I need to do, but I have not the first clue about how to go about that
bit.

Can anyone help, please?

Thanks
Marc.

Nov 16 '05 #2
Hi,

I dont think that will work, you are detecting the environment at runtime,
the conditionals are evaluated at compile time.

A possible solution would be define a preprocessor in the project
configuration, if you have a different project definition file in each
machine you can do what yuoui want, how factible this is would depend of
your project, if you add a new file or a new reference to one of the
projects you will need to do the same thing in the others, that is the only
( HUGE ) drawback I see. you can copy the source code though.

It probably is not a solution but may be a work around, it's the best I can
think of right now.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Marc Jennings" <Ma**********@community.nospam> wrote in message
news:p7********************************@4ax.com...
Hi there,

I am trying to get some conditional functionality into some of my
apps. I can use the format :

[Conditional("DEBUG")]
private void doStuff()
{
//do something
}

as expected, but I would like to makr it a little more useful in a
multi-machine environment.

I want to make tweaks to an application based up whether it is running
on my development laptop, the stage server or the live server, and
conditionals seem to be the most efficient way forward. However, I am
not sure how to go about defining an environment variable to trigger
the consitional method.

Here's what I *think* I want to do...
**snip unnecessary stuff**

string outputMessage;
string productionServer;

private string statusMessage()
{
outputMessage = "This application is running on " + Environment.HostName
+ ". ";
productionServer = "This is the production server. ";
isDevBox();
return outputMessage + productionServer;
}

[Conditional( ***HERE IS WHERE I AM COMING UNSTUCK****)]
private void checkIsDevBox()
{
//reset things like connection strings, data table names, development
queries, and whatever else I need
productionServer = "This is one of the development of stage servers.
";
}
**snip unnecessary stuff**


It seems that I need to check if Environment.Hostname matches one of
my two development server names, and if so, set a flag that
[Conditional] can work with. It's all very well to know this is what
I need to do, but I have not the first clue about how to go about that
bit.

Can anyone help, please?

Thanks
Marc.

Nov 16 '05 #3
Thanks for that. I guess I would have seen this if I had read the
docs properly.

It's not a problem, though. I'll just have to go back to the old
switch(Environment.MachineName) kind of thing (Which I probably prefer
anyway...)

Thanks again, both responders...

On Mon, 10 Jan 2005 09:53:12 -0500, "Nicholas Paldino [.NET/C# MVP]"
<mv*@spam.guard.caspershouse.com> wrote:
Marc,

You aren't going to be able to do this. The reason is that the
Conditional attribute is evaluated at compile-time, not run-time. It is
strictly for pre-processor values, not environment values.

If you want to do something based on the environment, you are going to
have to write code that will detect the environment you are in, and then
make the appropriate calls.

Hope this helps.


Nov 16 '05 #4
I would even suggest refactor the code into some type of factory pattern to
make the code more managable.

"Marc Jennings" wrote:
Thanks for that. I guess I would have seen this if I had read the
docs properly.

It's not a problem, though. I'll just have to go back to the old
switch(Environment.MachineName) kind of thing (Which I probably prefer
anyway...)

Thanks again, both responders...

On Mon, 10 Jan 2005 09:53:12 -0500, "Nicholas Paldino [.NET/C# MVP]"
<mv*@spam.guard.caspershouse.com> wrote:
Marc,

You aren't going to be able to do this. The reason is that the
Conditional attribute is evaluated at compile-time, not run-time. It is
strictly for pre-processor values, not environment values.

If you want to do something based on the environment, you are going to
have to write code that will detect the environment you are in, and then
make the appropriate calls.

Hope this helps.


Nov 16 '05 #5
Alternatively, you could just use different config files on you different
systems that contain the proper configuration for the environment. That way
you don't have to hard-code machine names or the such.

Ken
"Marc Jennings" <Ma**********@community.nospam> wrote in message
news:a5********************************@4ax.com...
Thanks for that. I guess I would have seen this if I had read the
docs properly.

It's not a problem, though. I'll just have to go back to the old
switch(Environment.MachineName) kind of thing (Which I probably prefer
anyway...)

Thanks again, both responders...

On Mon, 10 Jan 2005 09:53:12 -0500, "Nicholas Paldino [.NET/C# MVP]"
<mv*@spam.guard.caspershouse.com> wrote:
Marc,

You aren't going to be able to do this. The reason is that the
Conditional attribute is evaluated at compile-time, not run-time. It is
strictly for pre-processor values, not environment values.

If you want to do something based on the environment, you are going tohave to write code that will detect the environment you are in, and then
make the appropriate calls.

Hope this helps.

Nov 16 '05 #6
To add to Daniel's reply,

Create an object that encapsulates the differences in functionality that you
need between your dev environment and your production environment (and any
others). You can have multiple child classes, one for each environment, all
inheriting from the same interface.

Then, when your app starts, call a factory method to return a child object
that matches the environment. The factory method can either refer to the
config file to detect the environment or use machine name or whatever you
want (I prefer config file, but it's your call). After that, none of your
code needs to ever write a line of different code for "production" vs "dev
environment" .

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Marc Jennings" <Ma**********@community.nospam> wrote in message
news:a5********************************@4ax.com...
Thanks for that. I guess I would have seen this if I had read the
docs properly.

It's not a problem, though. I'll just have to go back to the old
switch(Environment.MachineName) kind of thing (Which I probably prefer
anyway...)

Thanks again, both responders...

On Mon, 10 Jan 2005 09:53:12 -0500, "Nicholas Paldino [.NET/C# MVP]"
<mv*@spam.guard.caspershouse.com> wrote:
Marc,

You aren't going to be able to do this. The reason is that the
Conditional attribute is evaluated at compile-time, not run-time. It is
strictly for pre-processor values, not environment values.

If you want to do something based on the environment, you are going tohave to write code that will detect the environment you are in, and then
make the appropriate calls.

Hope this helps.

Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by mux | last post: by
3 posts views Thread by Kumar | last post: by
10 posts views Thread by Dave | last post: by
5 posts views Thread by paulo | last post: by
1 post views Thread by Marek | last post: by
2 posts views Thread by John | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.