Hi! I want to improve my coding by using Select/Case instead of several If/ElseIf statements.
Depending on the value of $CRTN and $PKG, I want to return the following:
1 LOOSE CARTON
2 LOOSE CARTONS
1 CARTON ON 1 SKID
3 CARTONS ON 1 SKID
123 CARTONS ON 5 SKIDS
By CARTON(S), I mean box(es).By SKID(S), I mean pallet(s).
Both of the following are impossible solutions for my purpose, minding the English singular and plural:
"x LOOSE CARTON(S) ON y SKID(S)" If carton(s) are loose, then they are not on a skid(s).
"1 CARTON on >1 SKID" 1 carton does not fit on multiple skids.
"5 CARTONS on 4 SKIDS" the number of skids must be =>the number of cartons
In practice, $CRTN and $PKG refer to Excel (with error trapping for opening the Excel Application):
$CRTN=$oExcel.Activesheet.Cells(1,1).Value
$PKG=$oExcel.Activesheet.Cells(1,2).Value
But in theory, I just define the dims in SciTE4 editor, change the values each time for confirmation it does what I'm telling it to. Notepad must be open.
Then I rewrote the mess above using Select and Case
Then I rewrote Select and case using only 1 argument.
Is there a way to make the following code better/more elegant?
Is there an error trap(s) that I'm missing? Thanks in advance.
Depending on the value of $CRTN and $PKG, I want to return the following:
1 LOOSE CARTON
2 LOOSE CARTONS
1 CARTON ON 1 SKID
3 CARTONS ON 1 SKID
123 CARTONS ON 5 SKIDS
By CARTON(S), I mean box(es).By SKID(S), I mean pallet(s).
Both of the following are impossible solutions for my purpose, minding the English singular and plural:
"x LOOSE CARTON(S) ON y SKID(S)" If carton(s) are loose, then they are not on a skid(s).
"1 CARTON on >1 SKID" 1 carton does not fit on multiple skids.
"5 CARTONS on 4 SKIDS" the number of skids must be =>the number of cartons
In practice, $CRTN and $PKG refer to Excel (with error trapping for opening the Excel Application):
$CRTN=$oExcel.Activesheet.Cells(1,1).Value
$PKG=$oExcel.Activesheet.Cells(1,2).Value
But in theory, I just define the dims in SciTE4 editor, change the values each time for confirmation it does what I'm telling it to. Notepad must be open.
[ autoit ]
$Loose=MsgBox(4,"Loose?","Are the cartons on a skid?") $CRTN=1 $PKG=1 ;---If/ElseIf--------- WinActivate("Untitled - Notepad") If $Loose=6 Then If $CRTN=1 Then Send($CRTN & " LOOSE CARTON") ElseIf $CRTN>1 Then Send($CRTN & " LOOSE CARTONS") EndIf EndIf If $Loose=7 Then If $CRTN=1 Then If $PKG=1 Then Send($CRTN&" CARTON ON "&$PKG&" SKID") EndIf EndIf If $CRTN>1 Then If $PKG=1 Then Send($CRTN&" CARTONS ON "&$PKG&" SKID") ElseIf $PKG>1 Then Send($CRTN&" CARTONS ON "&$PKG&" SKIDS") EndIf EndIf EndIf
Then I rewrote the mess above using Select and Case
[ autoit ]
;----with Select/Case---- $Loose=MsgBox(4,"Loose?","Are the cartons on a skid?") $CRTN=1 $PKG=1 WinActivate("Untitled - Notepad") Select Case $Loose = 7 AND $CRTN=1 Send($CRTN & " LOOSE CARTON") Case $Loose =7 AND $CRTN>1 Send($CRTN & " LOOSE CARTONS") EndSelect WinActivate("Untitled - Notepad") Select Case $Loose = 6 AND $CRTN=1 AND $PKG=1 Send($CRTN&" CARTON ON "&$PKG&" SKID") Case $Loose =6 AND $CRTN >1 AND $PKG=1 Send($CRTN&" CARTONS ON "&$PKG&" SKID") Case $Loose =6 AND $CRTN >1 AND $PKG>1 Send($CRTN&" CARTONS ON "&$PKG&" SKIDS") EndSelect
Then I rewrote Select and case using only 1 argument.
[ autoit ]
;----with Select/Case but better---- $Loose=MsgBox(4,"Loose?","Are the cartons on a skid?") $CRTN=1 $PKG=1 WinActivate("Untitled - Notepad") Select Case $Loose = 7 AND $CRTN = 1 Send($CRTN & " LOOSE CARTON") Case $Loose = 7 AND $CRTN > 1 Send($CRTN & " LOOSE CARTONS") Case $Loose = 6 AND $CRTN = 1 AND $PKG = 1 Send($CRTN&" CARTON ON "&$PKG&" SKID") Case $Loose = 6 AND $CRTN > 1 AND $PKG = 1 Send($CRTN&" CARTONS ON "&$PKG&" SKID") Case $Loose = 6 AND $CRTN > 1 AND $PKG > 1 Send($CRTN&" CARTONS ON "&$PKG&" SKIDS") EndSelect
Is there a way to make the following code better/more elegant?
Is there an error trap(s) that I'm missing? Thanks in advance.