Combine if() with plugins.dilaog...

Find out how to get things done with Servoy. Post how YOU get things done with Servoy

Combine if() with plugins.dilaog...

Postby Kahuna » Fri Apr 24, 2009 11:16 am

Not sure if this has been discussed before - or if I'm storing up trouble for the future (I've always been a saver :wink: ) but this combining of if() and the dialog is much more readable that the format suggested in the sample code.

Where I need to ask the user a question with a dialog I've been using this:

Code: Select all
var thePressedButton = plugins.dialogs.showQuestionDialog('Warning', 'This SQL has NOT been applied to the Database yet - do you want to continue without applying it ?','OK', 'Cancel');
      if(thePressedButton =='OK'){
         //Get the current record index or do some work etc.
         }      }
      else {return;}

A technique I used in VB seems to work well here too:

Code: Select all
if('OK' == plugins.dialogs.showQuestionDialog('Warning', 'Lets see which button you press ?','OK', 'Cancel')){ //do some work}
else {return;}

Doesn't save a lot of code but is more readable IMHO.
(Servoy Version: 6.0.7 Win XP / 7 - SQL Server 2008 R2)
Ian Cordingley (Kahuna)
Kahuna
 
Posts: 1235
Joined: Thu Oct 26, 2006 1:39 am
Location: 1/2 NE UK 1/2 Olvera Spain

Re: Combine if() with plugins.dilaog...

Postby ROCLASI » Fri Apr 24, 2009 11:21 am

Hi Ian,

That is indeed more efficient with one exception.
When you use more buttons than OK/Cancel then you need to put the result in a variable (or use a switch statement).
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Re: Combine if() with plugins.dilaog...

Postby Kahuna » Fri Apr 24, 2009 11:45 am

ROCLASI wrote:Hi Ian,

That is indeed more efficient with one exception.
When you use more buttons than OK/Cancel then you need to put the result in a variable (or use a switch statement).


Aha - thanks Robert.
(Servoy Version: 6.0.7 Win XP / 7 - SQL Server 2008 R2)
Ian Cordingley (Kahuna)
Kahuna
 
Posts: 1235
Joined: Thu Oct 26, 2006 1:39 am
Location: 1/2 NE UK 1/2 Olvera Spain

Re: Combine if() with plugins.dilaog...

Postby agiletortoise » Fri Apr 24, 2009 1:31 pm

As a general rule, I think you also improve readability considerably by reversing your condition, and doing the following:

Code: Select all
if('OK' != plugins.dialogs.showQuestionDialog('Warning', 'Lets see which button you press ?','OK', 'Cancel'))
   return;

//do some work


You are really testing for a reason to exit, so the resulting action should logically be to trigger the return. Also, you save a level of indentation on your actual working code.

g.
Greg Pierce
Agile Tortoise
SAN Developer
http://www.agiletortoise.com
User avatar
agiletortoise
 
Posts: 278
Joined: Wed Oct 12, 2005 3:26 pm
Location: Texas, USA

Re: Combine if() with plugins.dilaog...

Postby Kahuna » Fri Apr 24, 2009 1:36 pm

agiletortoise wrote:As a general rule, I think you also improve readability considerably by reversing your condition, and doing the following:

Code: Select all
if('OK' != plugins.dialogs.showQuestionDialog('Warning', 'Lets see which button you press ?','OK', 'Cancel'))
   return;

//do some work


You are really testing for a reason to exit, so the resulting action should logically be to trigger the return. Also, you save a level of indentation on your actual working code.

g.


Your absolutely right Greg - I use both directions results dependant.
(Servoy Version: 6.0.7 Win XP / 7 - SQL Server 2008 R2)
Ian Cordingley (Kahuna)
Kahuna
 
Posts: 1235
Joined: Thu Oct 26, 2006 1:39 am
Location: 1/2 NE UK 1/2 Olvera Spain


Return to How To

Who is online

Users browsing this forum: No registered users and 5 guests