Page 1 of 2

Emulating a Case/Select Command

PostPosted: Sun Jan 19, 2014 7:37 pm
by Enigman
Has anyone developed a good reusable bit of code to emulate a Case/Select command? I would prefer to do it with native NB commands or a plug-in rather than a function.

Thanks.

Re: Emulating a Case/Select Command

PostPosted: Sun Jan 19, 2014 10:15 pm
by Tony Kroos
why don't u use IF(IFEx)...Else...EndIf statements ?

Re: Emulating a Case/Select Command

PostPosted: Sun Jan 19, 2014 11:02 pm
by Enigman
use IF(IFEx)...Else...EndIf statements


I've already done that. I was looking for something a little cleaner and more suited to being a subroutine. Or a plugin that has made a direct command for it. Without doing a lot of nesting, If blocks lack "elseif" and a global "else" case. Maybe one day NB will enable the Delphi Case/Select command? Dave? Any plans for that?

Thanks.

Re: Emulating a Case/Select Command

PostPosted: Mon Jan 20, 2014 12:16 am
by Tony Kroos
I personally never needed Select Case... u can post your if... else code block so maybe we could take a look and improve it if possible
p.s: try hpwCase command from hpwControl plugin

Re: Emulating a Case/Select Command

PostPosted: Mon Jan 20, 2014 2:53 pm
by Enigman
My current code is just the most simple stack of IfEx blocks:

Code: Select all
:invFieldUpdate
....... Update the venue field backgrounds
IfEx "[db.Inventory.Venue] = [#34]All Resonant Energies[#34]"
     SetObjectFill "invVenue" "59,207,59" "Solid" "False"
EndIf
IfEx "[db.Inventory.Venue] = Amazon"
     SetObjectFill "invVenue" "255,166,27" "Solid" "False"
EndIf
IfEx "[db.Inventory.Venue] = [#34]eBay - Resonant Energies[#34]"
     SetObjectFill "invVenue" "0,162,160" "Solid" "False"
EndIf
IfEx "[db.Inventory.Venue] = [#34]eBay - Enigma Treasures[#34]"
     SetObjectFill "invVenue" "64,64,255" "Solid" "False"
EndIf
IfEx "[db.Inventory.Venue] = [#34]Web - Resonant Energies[#34]"
     SetObjectFill "invVenue" "196,46,198" "Solid" "False"
EndIf
IfEx "[db.Inventory.Venue] = [#34]All Venues[#34]"
     SetObjectFill "invVenue" "255,255,255" "Solid" "False"
EndIf

Return


There are only six possible values in this example, so I don't need an "Else" option. If there was an "Else" option then I would have to nest each IfEx inside the previous one.

I just like Select/Case statements because they are much smaller.

Thanks.

Re: Emulating a Case/Select Command

PostPosted: Mon Jan 20, 2014 3:40 pm
by dec
Hello,

Enigman wrote:I just like Select/Case statements because they are much smaller.

Smaller?

22 lines of code:

Code: Select all
:invFieldUpdate
  Case "[db.Inventory.Venue]"
    IfCase "[#34]All Resonant Energies[#34]"
      SetObjectFill "invVenue" "59,207,59" "Solid" "False"
    EndIfCase 
    IfCase "Amazon"
      SetObjectFill "invVenue" "255,166,27" "Solid" "False"
    EndIfCase 
    IfCase "[#34]eBay - Resonant Energies[#34]"
      SetObjectFill "invVenue" "0,162,160" "Solid" "False"
    EndIfCase 
    IfCase "[#34]eBay - Enigma Treasures[#34]"
      SetObjectFill "invVenue" "64,64,255" "Solid" "False"
    EndIfCase
    IfCase "[#34]Web - Resonant Energies[#34]"
      SetObjectFill "invVenue" "196,46,198" "Solid" "False"
    EndIfCase
    IfCase "[#34]All Venues[#34]"
      SetObjectFill "invVenue" "255,255,255" "Solid" "False"
    EndIfCase
  EndCase
Return

Of course the above code is just "fictional", but an aproximation of how a "case statement" can be.

20 lines of code:

Code: Select all
:invFieldUpdate
  IfEx "[db.Inventory.Venue] = [#34]All Resonant Energies[#34]"
    SetObjectFill "invVenue" "59,207,59" "Solid" "False"
  EndIf
  IfEx "[db.Inventory.Venue] = Amazon"
    SetObjectFill "invVenue" "255,166,27" "Solid" "False"
  EndIf
  IfEx "[db.Inventory.Venue] = [#34]eBay - Resonant Energies[#34]"
    SetObjectFill "invVenue" "0,162,160" "Solid" "False"
  EndIf
  IfEx "[db.Inventory.Venue] = [#34]eBay - Enigma Treasures[#34]"
    SetObjectFill "invVenue" "64,64,255" "Solid" "False"
  EndIf
  IfEx "[db.Inventory.Venue] = [#34]Web - Resonant Energies[#34]"
    SetObjectFill "invVenue" "196,46,198" "Solid" "False"
  EndIf
  IfEx "[db.Inventory.Venue] = [#34]All Venues[#34]"
    SetObjectFill "invVenue" "255,255,255" "Solid" "False"
  EndIf
Return

So the "If" version is 2 lines less than the "Case" version. ;)

I can imagine some plugin action which implement some way of a "case statement". The problem I mainly found is that the "code part" to be executed probably need to be subroutines (or just an action, or anyway not "If", "IfEx" or other actions like that), and therefore the "case statement" appear "disgregated" in various publication parts, which is something that maybe do not appear too good in this particular case.

On the other hand you say that do not want to use functions, but probably can be a good approach here, since the code that use the function appear less complex, leting the function the "If" statements checks. Well. ;)

Re: Emulating a Case/Select Command

PostPosted: Mon Jan 20, 2014 4:07 pm
by Enigman
Actually, your fictional code example is not correct. The actual Select case would be more like:

Code: Select all
:invFieldUpdate
  Select "[db.Inventory.Venue]"
    Case "All Resonant Energies":
      SetObjectFill "invVenue" "59,207,59" "Solid" "False"
    Case "Amazon":
      SetObjectFill "invVenue" "255,166,27" "Solid" "False"
    Case "eBay - Resonant Energies":
      SetObjectFill "invVenue" "0,162,160" "Solid" "False"
    Case "eBay - Enigma Treasures":
      SetObjectFill "invVenue" "64,64,255" "Solid" "False"
    Case "Web - Resonant Energies":
      SetObjectFill "invVenue" "196,46,198" "Solid" "False"
    Case Else:
      SetObjectFill "invVenue" "255,255,255" "Solid" "False"
  EndSelect
Return


... which is only 16 lines. In a real Select/Case, embedded IF statements are not required, nor are EndCase marks. It simply ends the code block when it encounters the next Case. Notice also that the last case is always "Case Else:" which eliminates the need for lots of IF block nesting. In reality I would probably not put that in a subroutine all by itself if I had that command, which reduces it to 14 lines. For esthetics and readability I would expand it with blanks like this:

Code: Select all
Select "[db.Inventory.Venue]"

    Case "All Resonant Energies":
      SetObjectFill "invVenue" "59,207,59" "Solid" "False"

    Case "Amazon":
      SetObjectFill "invVenue" "255,166,27" "Solid" "False"

    Case "eBay - Resonant Energies":
      SetObjectFill "invVenue" "0,162,160" "Solid" "False"

    Case "eBay - Enigma Treasures":
      SetObjectFill "invVenue" "64,64,255" "Solid" "False"

    Case "Web - Resonant Energies":
      SetObjectFill "invVenue" "196,46,198" "Solid" "False"

    Case Else:
      SetObjectFill "invVenue" "255,255,255" "Solid" "False"

EndSelect


Much cleaner that way.

I don't like functions in the NB implementation because they must be located in the NB directory instead of being located in the application directory during development. It has been my experience that over the years changing machines and NB installations sometimes 'loses" functions and then I am screwed for future compiles of the application.

Thanks.

Re: Emulating a Case/Select Command

PostPosted: Mon Jan 20, 2014 4:49 pm
by dec
Hello,

Yes; probably you have reason that the "EndCase" is not really needed. ;)

Re: Emulating a Case/Select Command

PostPosted: Mon Jan 20, 2014 5:30 pm
by Enigman
The syntax I quoted was from languages like Javascript, C++, PHP, ASP, and others. It's pretty well standardized at the 3GL and OOP level.

Re: Emulating a Case/Select Command

PostPosted: Mon Jan 20, 2014 5:38 pm
by Gaev
Enigman:

I don't like functions in the NB implementation because they must be located in the NB directory instead of being located in the application directory during development.
f.y.i. from the Help file for the Call command ...
Call "function name" "function parameters (optional)"

function name
The name of the function to execute. If the desired function is located in a sub folder then the folder's name must also be included. For example: "Math\InterestRate"
... perhaps this might change your mind about using Functions (a very powerful feature of NeoBook).

It is too bad that you can not supply parameters with the GoSub commands ... but you might consider something like this ...
Code: Select all
:invFieldUpdate
SetVar "[delim]" "~~~"
SetVar "[IfObjectFill_Match]" "No"

SetVar "[IfObjectFill_Values]" "[db.Inventory.Venue][delim]All Resonant Energies[delim]invVenue[delim]59,207,59[delim]Solid[delim]False""
GoSub "IfObjectFill"

SetVar "[IfObjectFill_Values]" "[db.Inventory.Venue][delim]Amazon[delim]invVenue[delim]255,166,27[delim]Solid[delim]False""
GoSub "IfObjectFill"

... etc. etc. ...

SetVar "[IfObjectFill_Values]" "[db.Inventory.Venue][delim]Web - Resonant Energies[delim]invVenue[delim]196,46,198[delim]Solid[delim]False""
GoSub "IfObjectFill"

If ""[IfObjectFill_Match]" "=" "No"
   SetObjectFill "invVenue" "255,255,255" "Solid" "False"
EndIf
Return
... same number of commands in the calling routine ... if you need help with the IfObjectFill subroutine, ask here.

Re: Emulating a Case/Select Command

PostPosted: Mon Jan 20, 2014 6:11 pm
by Enigman
Gaev,

Call "function name" "function parameters (optional)"

function name
The name of the function to execute. If the desired function is located in a sub folder then the folder's name must also be included. For example: "Math\InterestRate"

... perhaps this might change your mind about using Functions (a very powerful feature of NeoBook).


Yeah, but the sub folders are also within the function directory under the NB Install directory. Same problem. I used functions extensively in one app and it was a hassle keeping the backups straight after system changes. I haven't done anything that really needed functions since then. It's not hard to parameterize subroutines if needed. Just dedicate some vars to the subroutine.

Thanks.

Re: Emulating a Case/Select Command

PostPosted: Mon Jan 20, 2014 6:27 pm
by Gaev
Enigman:
Yeah, but the sub folders are also within the function directory under the NB Install directory.

Both these commands worked for me ...
Code: Select all
Call "c:\gk\Neo5\gkLab\HelloWorld"
Call "[PubDir]HelloWorld"
... just save the Function file in the same folder as your pub ... and don't specify any extension.

Re: Emulating a Case/Select Command

PostPosted: Mon Jan 20, 2014 6:40 pm
by mishem
And if 100 values​​? Also if?

Something like this.

Code: Select all
SetVar "[Venue]" "All Resonant Energies,Amazon,eBay - Resonant Energies,eBay - Enigma Treasures,Web - Resonant Energies,All Venues"
SetVar "[value]" "59,207,59|255,166,27|0,162,160|64,64,255|196,46,198|255,255,255"

StrParse "[Venue]" "," "[Venue]" ""
StrParse "[value]" "|" "[value]" ""

Loop "1" "6" "[i]"
    IfEx "[db.Inventory.Venue] = [#34][Venue[i]][#34]"
         SetObjectFill "invVenue" "[value[i]]" "Solid" "False"
         ExitLoop
    EndIf
EndLoop

DeleteArray "[Venue]" "All"
DeleteArray "[value]" "All"

Re: Emulating a Case/Select Command

PostPosted: Mon Jan 20, 2014 6:49 pm
by Enigman
Both these commands worked for me ...

Call "c:\gk\Neo5\gkLab\HelloWorld"
Call "[PubDir]HelloWorld"


Interesting. That wasn't the way the documentation read and the function library in NB doesn't use external folders. That makes it more attractive.

Thanks.

Re: Emulating a Case/Select Command

PostPosted: Tue Jan 21, 2014 4:14 am
by dec
Hello,

Enigman wrote:The syntax I quoted was from languages like Javascript, C++, PHP, ASP, and others. It's pretty well standardized at the 3GL and OOP level.


My syntax is more based in the current NeoBook syntax.