NETCoreBlockly icon indicating copy to clipboard operation
NETCoreBlockly copied to clipboard

Switch blocks

Open djaus2 opened this issue 9 months ago • 9 comments

Unless there is already a way, it would be nice to have switch blocks based upon ordinals. Supposed one issue might be the number of switches. Today I implemented the equivalent with nested if then else blocks. Keep up the good work! 👍

djaus2 avatar Mar 25 '25 00:03 djaus2

I am not sure that I understand what it says. Could you please give an example ?

ignatandrei avatar Mar 25 '25 05:03 ignatandrei

Image An example

djaus2 avatar Mar 25 '25 20:03 djaus2

Didn't mean to close this.

djaus2 avatar Mar 25 '25 20:03 djaus2

I understand now. Thinking....

ignatandrei avatar Mar 26 '25 05:03 ignatandrei

Thx for reopenning this and considering it. I have worked out a work around for now, not yet implemented. I can add a new ASP.NET controller method that takes a csv list of ordinals for the required commands along with the menu parameter which implements the switch-case funstionality there. ... The "beauty of being able to adhocly add more controller methords..

Even better, take the string at the start of the block above and look up the command names using the menu parameter and call that comamnd. The new version of Softata NetCporeBlockly has lookup capability for commands. Nb: For simplicity, the lookup selects the named command that either contains the part of the csv string when split into its components or the commnd name that is contained in the lookup value._ A "lazy" search.

djaus2 avatar Mar 26 '25 05:03 djaus2

The latter was simple to implement:

        public IActionResult ActionDeviceCmdindexfrmCSVlistNoParam(DeviceInstance deviceInstance, string csv, byte index)
        {
            string[] cmds = csv.Split(',');
            if (index >= cmds.Length)
            {
                return BadRequest("Index out of range");
            }
            byte subCmd = LookUpGenericCmd(deviceInstance.DeviceType, cmds[index]);
            string result = sharedService.ActionDeviceCmdwithByteArrayParams((int)deviceInstance.DeviceType, HttpContext, Client, deviceInstance.ListLinkId, subCmd);
            return Ok(result);
        }

Is only a few obvious lines different from ActionDeviceCmdNoParam() Actually it's a little more complex, (not shown here) but only marginally, because the csv string has a colon seperated part at start for the menu heading.

switch-case would be useful in the longer term though, but I understand it could be complex.

djaus2 avatar Mar 26 '25 06:03 djaus2

Here are the blocks thus (implemented with 2nd scenario):

Image

djaus2 avatar Mar 26 '25 07:03 djaus2

Comment. Note found that variables used in a function are global. Hence menu2 as menu is in the Menu with MenuCSV() function etc. Might be "nice" if function variables weren't gobal

djaus2 avatar Mar 26 '25 07:03 djaus2

Oh I see that the following block can do it, I think. Once I figured out how to and more cases:

Image

djaus2 avatar Mar 26 '25 09:03 djaus2