iLogic IfThenElse query

iLogic IfThenElse query

dleesuk
Advocate Advocate
589 Views
3 Replies
Message 1 of 4

iLogic IfThenElse query

dleesuk
Advocate
Advocate
Good Morning All,

I have a general question regarding iLogic.

I am quite new to programming and I'm sure someone on here could help.

I have quite a long IfThen statement which branches based on a range of sizes (see part of it below):


If PinOD = 25 mm Then 'Pin Dia = 25mm
SlotLocation = 16 mm
NonRotBase = 5 mm
SlotWidth = 7.9 mm
RotDia = 17 mm

Else If PinOD > 25 And PinOD <=30 Then 'Pin Dia Range = 26-30mm
SlotLocation = 17 mm
NonRotBase = 6 mm
SlotWidth = 10.2 mm
RotDia = 17 mm

Else If PinOD >=32 And PinOD <=38 Then 'Pin Dia Range = 32-38mm
SlotLocation = 21 mm
NonRotBase = 6 mm
SlotWidth = 10.2 mm
RotDia = 25 mm

Else If PinOD >=39 And PinOD <=40 Then 'Pin Dia Range = 39-40mm
SlotLocation = 25 mm
NonRotBase = 7 mm
SlotWidth = 12.2 mm
RotDia = 32 mm

Else If PinOD >=44 And PinOD <=51 Then 'Pin Dia Range = 44-51mm
SlotLocation = 25 mm
NonRotBase = 9 mm
SlotWidth = 12.2 mm
RotDia = 37 mm

Else If PinOD >=54 And PinOD <=64 Then 'Pin Dia Range = 54-64mm
SlotLocation = 28 mm
NonRotBase = 12 mm
SlotWidth = 14.2 mm
RotDia = 47 mm"

.... the list continues another 22 times to cover the range of pins


I have considered the case construct, but this may turn out to be just as long.

Is there an way of compressing the code to a more elegant solution than the lengthy option(s)??

Thank you


Regards

Darren

Regards

Darren
0 Likes
Accepted solutions (2)
590 Views
3 Replies
Replies (3)
Message 2 of 4

Owner2229
Advisor
Advisor
Accepted solution

Hi, if the values can't be calculated (eighter directly or with some kind of function) from the PinOD value, than I'm affraid the case select is the very best you can get. But you can format it as a table, so it won't take as much space. E.g.: Here is the example you've posted, formated as a table:

 

Select Case PinOD

Case 25: SlotLocation = 16: NonRotBase = 5: SlotWidth = 7.9: RotDia = 17 Case 26 To 30: SlotLocation = 17: NonRotBase = 6: SlotWidth = 10.2: RotDia = 17 Case 32 To 38: SlotLocation = 21: NonRotBase = 6: SlotWidth = 10.2: RotDia = 25 Case 39 To 40: SlotLocation = 25: NonRotBase = 7: SlotWidth = 12.2: RotDia = 32 Case 44 To 51: SlotLocation = 25: NonRotBase = 9: SlotWidth = 12.2: RotDia = 37 Case 54 To 64: SlotLocation = 28: NonRotBase = 12: SlotWidth = 14.2: RotDia = 47

End Select 

 

Not only it is 5-times shorter, but also way easier to read.

Also, case select is way faster to compute (if used instead of multiple If > Then statements).

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 3 of 4

dleesuk
Advocate
Advocate

Hi Owner2229,

 

Thank you for your response.

 

I thought CASE would be the way forward, I just needed a little shove.

 

 

 

Regards

 

Darren


Regards

Darren
0 Likes
Message 4 of 4

Owner2229
Advisor
Advisor
Accepted solution

You're welcomed Smiley Happy

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes