# More elegant/effective way to use Select/Case

## Recommended Posts

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.

```\$Loose=MsgBox(4,"Loose?","Are the cartons on a skid?")
\$CRTN=1
\$PKG=1

;---If/ElseIf---------
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

```;----with Select/Case----
\$Loose=MsgBox(4,"Loose?","Are the cartons on a skid?")
\$CRTN=1
\$PKG=1

Select
Case \$Loose = 7 AND \$CRTN=1
Send(\$CRTN & " LOOSE CARTON")
Case \$Loose =7 AND \$CRTN>1
Send(\$CRTN & " LOOSE CARTONS")
EndSelect

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.

```;----with Select/Case but better----
\$Loose=MsgBox(4,"Loose?","Are the cartons on a skid?")
\$CRTN=1
\$PKG=1
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.

##### Share on other sites

Looks alright to me, you could use a function I suppose and tuck it away somewhere.

```\$Loose = MsgBox(4, "Loose?", "Are the cartons on a skid?")

\$CRTN = 1

\$PKG = 1

Switch _Select(\$Loose, \$CRTN)
Case  1
Send(\$CRTN & " LOOSE CARTON")
Case 2
Send(\$CRTN & " LOOSE CARTONS")
Case 3
Send(\$CRTN & " CARTON ON " & \$PKG & " SKID")
Case 4
Send(\$CRTN & " CARTONS ON " & \$PKG & " SKID")
Case 5
Send(\$CRTN & " CARTONS ON " & \$PKG & " SKIDS")
EndSwitch

Func _Select(\$Loose, \$CRTN)

Select
Case \$Loose = 7 And \$CRTN = 1
Return 1
Case \$Loose = 7 And \$CRTN > 1
Return 2
Case \$Loose = 6 And \$CRTN = 1 And \$PKG = 1
Return 3
Case \$Loose = 6 And \$CRTN > 1 And \$PKG = 1
Return 4
Case \$Loose = 6 And \$CRTN > 1 And \$PKG > 1
Return 5
EndSelect
EndFunc   ;==>_Select```

Monkey's are, like, natures humans.

##### Share on other sites

Thanks John and guinness.

and sorry i didn't indent the code in my OP.

##### Share on other sites

JohnOne, I have two more questions.

How come there is no need to include \$PKG these two lines?

`Switch _Select(\$Loose, \$CRTN)`

and

`Func _Select(\$Loose, \$CRTN)`

Also, how do I place the function out of the way of the main body of the script?

For example:

```\$Loose = MsgBox(4, "Loose?", "Are the cartons on a skid?")
\$CRTN = 1
\$PKG = 1
code
code
;send carton and skid count to Notepad
_Select(\$Loose,\$CRTN)
code
code
Msgbox(0,"Done","Finished!")

Switch _Select(\$Loose, \$CRTN)
Case 1
Send(\$CRTN & " LOOSE CARTON")
Case 2
Send(\$CRTN & " LOOSE CARTONS")
Case 3
Send(\$CRTN & " CARTON ON " & \$PKG & " SKID")
Case 4
Send(\$CRTN & " CARTONS ON " & \$PKG & " SKID")
Case 5
Send(\$CRTN & " CARTONS ON " & \$PKG & " SKIDS")
EndSwitch

Func _Select(\$Loose, \$CRTN)

Select
Case \$Loose = 7 And \$CRTN = 1
Return 1
Case \$Loose = 7 And \$CRTN > 1
Return 2
Case \$Loose = 6 And \$CRTN = 1 And \$PKG = 1
Return 3
Case \$Loose = 6 And \$CRTN > 1 And \$PKG = 1
Return 4
Case \$Loose = 6 And \$CRTN > 1 And \$PKG > 1
Return 5
EndSelect
EndFunc ;==>_Select```

##### Share on other sites

Never noticed it, was just an example, just add it to both after another comma.

Put it in a file called MyFuncs.au3 or something and add #include "MyFuncs.au3"

Monkey's are, like, natures humans.

Thank you, both!

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...