PC Review


Reply
Thread Tools Rate Thread

[Conditional] question

 
 
Marc Jennings
Guest
Posts: n/a
 
      10th Jan 2005
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.
 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      10th Jan 2005
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]
- (E-Mail Removed)

"Marc Jennings" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> 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.



 
Reply With Quote
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      10th Jan 2005
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" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> 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.



 
Reply With Quote
 
Marc Jennings
Guest
Posts: n/a
 
      10th Jan 2005
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]"
<(E-Mail Removed)> 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.


 
Reply With Quote
 
=?Utf-8?B?RGFuaWVsIEppbg==?=
Guest
Posts: n/a
 
      10th Jan 2005
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]"
> <(E-Mail Removed)> 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.

>
>

 
Reply With Quote
 
Ken Kolda
Guest
Posts: n/a
 
      10th Jan 2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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]"
> <(E-Mail Removed)> 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.

>



 
Reply With Quote
 
Nick Malik [Microsoft]
Guest
Posts: n/a
 
      12th Jan 2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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]"
> <(E-Mail Removed)> 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.

>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Conditional Min/Max Question? Mike Microsoft Excel Programming 0 9th Apr 2011 02:35 AM
Conditional sum question Isis Microsoft Excel Misc 1 25th Apr 2010 01:33 PM
Re: Conditional Question H Microsoft Excel New Users 0 19th Jun 2009 06:11 PM
Conditional maybe IF question =?Utf-8?B?WnNvbHQgU3phYsOz?= Microsoft Excel Misc 3 10th Dec 2006 09:01 PM
question about conditional sum jinvictor Microsoft Excel Misc 1 8th Jun 2006 08:03 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:59 PM.