473,216 Members | 1,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,216 software developers and data experts.

[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 1535
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jouke Langhout | last post by:
Hello all! For quite some time now, I've got the following problem: Access won't close properly when a user closes the application. An ACCESS process stays active and that process can only be...
1
by: ammarton | last post by:
Hello all...I'm a bit new to working with Macros in Access so forgive me if the terminology I use is not accurate. To preface this, basically I am using a form on a replicated database so the...
4
by: mux | last post by:
Hi I found out that the following piece of code throws an error. 1 #include "stdio.h" 2 3 int main() 4 { 5 int a,b; 6 a= 10;
3
by: Kumar | last post by:
Hi Folks, I have a question regarding conditional hyperlink in datagrid. I want to display Hyperlink if my QID values in (1,4,5,6) other wise i want to display just Qdescription with out...
10
by: Dave | last post by:
I'm a C++ programmer of many years, trying to get my feet wet in C#. I have a question about conditional compilation. In C++, I would sometimes define a constant in an include file, and then...
5
by: paulo | last post by:
Can anyone please tell me how the C language interprets the following code: #include <stdio.h> int main(void) { int a = 1; int b = 10; int x = 3;
1
by: Marek | last post by:
I use VS2005 with framework 2.0 and I just found a behavior I consider odd. Here is the code that illustrates th eproblem: public static void MethodA() { MethodB() } #if DEBUG
3
by: somenath | last post by:
Hi All, I have one question regarding the conditional operator. In the draft C99 standard it is mentioned that "1 The following are the sequence points described in 5.1.2.3: -- The call to a...
2
by: John | last post by:
Hi there, I've just been reading "ASP.Net 2.0 - Master Pages: Tips, Tricks, and Traps" by K. Scott Allen (http://odetocode.com/Articles/450.aspx) and discovered how rebasing works with URL's in...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.