473,386 Members | 1,654 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,386 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 1547
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...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.