Code Red > VB(A)

fix Nested If-Then-Statement

(1/2) > >>

jtm2020hyo:
hello everyone.

 

Probably I found a bug with the VBScript. I was creating and experimenting a lot with the VBScript function if-then-elseif-else statement and I found something that before saving my drawing was working, but know that I was checking again simply stop working.

 

 



 



I was linking multiple functions to the function name "NAME_BY_OBJECT" which generates a different name per each different object. In images 1 and 2, everything is correct...

 

... but something weird happens with I add the object type "Hanger":

 





 

... again everything works correct, but just with hangers, but I was checking the cable trays and cable trays fitting this happens:

 




 

 

I do not what happens here.

 Then:

1) am I doing something bad?

2)if yes, how can I fix this?

3) if not, What Is wrong, and what alternatives exist?

 

PD: attached files


before:


--- Code: ---
If "[ObjectType]" = "Cable Tray" Then
RESULT = "[NAME_CABLE_TRAY]"

ElseIf "[ObjectType]" = "Cable Tray Fitting" Then
RESULT = "[NAME_CABLE_TRAY_FITTING]"

Else
RESULT = "NA"

End If
--- End code ---

 
after:

 
--- Code: ---
If "[ObjectType]" = "Cable Tray" Then
RESULT = "[NAME_CABLE_TRAY]"

ElseIf "[ObjectType]" = "Cable Tray Fitting" Then
RESULT = "[NAME_CABLE_TRAY_FITTING]"

ElseIf "[ObjectType]" = "Hanger" Then
RESULT = "[NAME_HANGER]"

Else
RESULT = "NA"

End If
--- End code ---



 

JohnK:
I don't do a lot of VB script stuff so you will have to tell me if this is legal or not.

Nested IF-Then statements are bad programming. You should always avoid using them as much as possible. And you should never use an ELSE statement after a return statement. Although, I don't think the "RESULT" is necessarily a "return" statement in VB script so this is why I said you need to tell me if the code snip I post is legal or not.

You are better off with small IF statements like below. You establish a default value for your variable (if you absolutely need one) and you use small IF statements like this. The theory is that when, and if, the RESULT keyword were a return (in which case you'd omit the first line) the code would stop executing when an if statement became true.


IF the RESULT keyword is not a "return" statement then you should be using something like:

--- Code: ---RESULT = "NA"
If "[ObjectType]" = "Cable Tray" Then
RESULT = "[NAME_CABLE_TRAY]"
End If

If "[ObjectType]" = "Cable Tray Fitting" Then
RESULT = "[NAME_CABLE_TRAY_FITTING]"
End If

If "[ObjectType]" = "Hanger" Then
RESULT = "[NAME_HANGER]"
End If
--- End code ---

However, if the RESULT keyword is a "return" statement then the code would look like this:


--- Code: ---If "[ObjectType]" = "Cable Tray" Then
RESULT = "[NAME_CABLE_TRAY]"
End If

If "[ObjectType]" = "Cable Tray Fitting" Then
RESULT = "[NAME_CABLE_TRAY_FITTING]"
End If

If "[ObjectType]" = "Hanger" Then
RESULT = "[NAME_HANGER]"
End If
--- End code ---


Otherwise, my guess would be there is a limit as to the elseif statements, meaning you are only allowed one.

jtm2020hyo:

--- Quote from: John Kaul (Se7en) on May 24, 2020, 08:24:39 PM ---I don't do a lot of VB script stuff so you will have to tell me if this is legal or not.

Nested IF-Then statements are bad programming. You should always avoid using them as much as possible. And you should never use an ELSE statement after a return statement. Although, I don't think the "RESULT" is necessarily a "return" statement in VB script so this is why I said you need to tell me if the code snip I post is legal or not.

You are better off with small IF statements like below. You establish a default value for your variable (if you absolutely need one) and you use small IF statements like this. The theory is that when, and if, the RESULT keyword were a return (in which case you'd omit the first line) the code would stop executing when an if statement became true.


IF the RESULT keyword is not a "return" statement then you should be using something like:

--- Code: ---RESULT = "NA"
If "[ObjectType]" = "Cable Tray" Then
RESULT = "[NAME_CABLE_TRAY]"
End If

If "[ObjectType]" = "Cable Tray Fitting" Then
RESULT = "[NAME_CABLE_TRAY_FITTING]"
End If

If "[ObjectType]" = "Hanger" Then
RESULT = "[NAME_HANGER]"
End If
--- End code ---

However, if the RESULT keyword is a "return" statement then the code would look like this:


--- Code: ---If "[ObjectType]" = "Cable Tray" Then
RESULT = "[NAME_CABLE_TRAY]"
End If

If "[ObjectType]" = "Cable Tray Fitting" Then
RESULT = "[NAME_CABLE_TRAY_FITTING]"
End If

If "[ObjectType]" = "Hanger" Then
RESULT = "[NAME_HANGER]"
End If
--- End code ---


Otherwise, my guess would be there is a limit as to the elseif statements, meaning you are only allowed one.

--- End quote ---

sorry for the late answer. I am testing a lot of things to solve this problem, I was testing your code, your code works, but just when is not a nested function, this still does not solve the nested if-statement because probably exist any undocumented limit in the number of characters that support each function or similar limit and I am trying to found it. So I will keep updating this post in the future.

PD: Thanks for your help and thanks for being a great moderator.

jtm2020hyo:

--- Quote from: John Kaul (Se7en) on May 24, 2020, 08:24:39 PM ---I don't do a lot of VB script stuff so you will have to tell me if this is legal or not.

Nested IF-Then statements are bad programming. You should always avoid using them as much as possible. And you should never use an ELSE statement after a return statement. Although, I don't think the "RESULT" is necessarily a "return" statement in VB script so this is why I said you need to tell me if the code snip I post is legal or not.

You are better off with small IF statements like below. You establish a default value for your variable (if you absolutely need one) and you use small IF statements like this. The theory is that when, and if, the RESULT keyword were a return (in which case you'd omit the first line) the code would stop executing when an if statement became true.


IF the RESULT keyword is not a "return" statement then you should be using something like:

--- Code: ---RESULT = "NA"
If "[ObjectType]" = "Cable Tray" Then
RESULT = "[NAME_CABLE_TRAY]"
End If

If "[ObjectType]" = "Cable Tray Fitting" Then
RESULT = "[NAME_CABLE_TRAY_FITTING]"
End If

If "[ObjectType]" = "Hanger" Then
RESULT = "[NAME_HANGER]"
End If
--- End code ---

However, if the RESULT keyword is a "return" statement then the code would look like this:


--- Code: ---If "[ObjectType]" = "Cable Tray" Then
RESULT = "[NAME_CABLE_TRAY]"
End If

If "[ObjectType]" = "Cable Tray Fitting" Then
RESULT = "[NAME_CABLE_TRAY_FITTING]"
End If

If "[ObjectType]" = "Hanger" Then
RESULT = "[NAME_HANGER]"
End If
--- End code ---


Otherwise, my guess would be there is a limit as to the elseif statements, meaning you are only allowed one.

--- End quote ---


Hi again, I finally found a possible solution.

your code posted here:


--- Code: ---RESULT = "NA"
If "[ObjectType]" = "Cable Tray" Then
RESULT = "[NAME_CABLE_TRAY]"
End If

If "[ObjectType]" = "Cable Tray Fitting" Then
RESULT = "[NAME_CABLE_TRAY_FITTING]"
End If

If "[ObjectType]" = "Hanger" Then
RESULT = "[NAME_HANGER]"
End If
--- End code ---

... works and solve the problem but I need to insert this structure in each every formula function, but I need to add an additional comparison statement and filter objects for finally reduce the number of caracters.

... something like this:



--- Code: ---RESULT = "NA"
If ("[ObjectType]") = ("Cable Tray") And ("ParType" = "Trunking") Then
RESULT = "[NAME_CABLE_TRAY]"
End If

If ("[ObjectType]") = ("Cable Tray Fitting") And ("ParType" = "Elbow")Then
RESULT = "[NAME_CABLE_TRAY_FITTING]"
End If

If ("[ObjectType]") = ("Hanger") Then ("Anchor_System" = "Electric")
RESULT = "[NAME_HANGER]"
End If
--- End code ---

but this need to analyze each formula and loss completely their beauty, for that motive I was adding the If ElseIf Else End If Statement in the ending"



--- Code: ---If "[ObjectType]" = "Cable Tray" Then
RESULT = "[NAME_CABLE_TRAY]"

ElseIf "[ObjectType]" = "Cable Tray Fitting" Then
RESULT = "[NAME_CABLE_TRAY_FITTING]"

ElseIf "[ObjectType]" = "Hanger" Then
RESULT = "[NAME_HANGER]"
Else
        RESULT = "NA"
End If
--- End code ---


This was the original script and should work, in theory, but it doesn't. The Else statement, in the end, shouldn't make the code work? I mean, Else in the end should equivalent to the "RESULT=" at the start. I am doing something bad?

JohnK:
Glad my code works.

I wouldn't worry too much about an undocumented limit in the IF-IFELSE-ELSE statement; I would just avoid using them. They are typically a very bad programming method.

Try to replace this:

--- Code: ---If(boolean_expression) Then
   Statement n
ElseIf (boolean_expression) Then
   Statement n
ElseIf (boolean_expression) Then
   Statement n
Else
   Statement n
End If
--- End code ---

With this methodology:

--- Code: ---Select Case expression
   Case expressionlistN
      statementN
   Case expressionlistN
      statementN
  Case Else
      elsestatementN
End Select
--- End code ---

You are correct, the ELSE statement is meant to offer a "default" value.

The code block I posted may not be as `pretty' but it reads so much cleaner (there is little doubt as to what your intentions are). That is to say (from the standpoint of the processor):
"VARIABLE SET. TEST: TEST-FAILED. ... TEST: VARIABLE REDEFINED"

Navigation

[0] Message Index

[#] Next page

Go to full version