Author Topic: fix Nested If-Then-Statement  (Read 591 times)

0 Members and 1 Guest are viewing this topic.

jtm2020hyo

  • Newt
  • Posts: 140
fix Nested If-Then-Statement
« on: May 24, 2020, 02:45:33 AM »
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: [Select]

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

 
after:

 
Code: [Select]

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



 

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9463
Re: fix Nested If-Then-Statement
« Reply #1 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: [Select]
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

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

Code: [Select]
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


Otherwise, my guess would be there is a limit as to the elseif statements, meaning you are only allowed one.
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

jtm2020hyo

  • Newt
  • Posts: 140
Re: fix Nested If-Then-Statement
« Reply #2 on: May 26, 2020, 02:05:24 AM »
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: [Select]
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

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

Code: [Select]
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


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

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

  • Newt
  • Posts: 140
Re: fix Nested If-Then-Statement
« Reply #3 on: May 26, 2020, 01:15:27 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: [Select]
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

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

Code: [Select]
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


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


Hi again, I finally found a possible solution.

your code posted here:

Code: [Select]
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

... 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: [Select]
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

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: [Select]
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


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?

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9463
Re: fix Nested If-Then-Statement
« Reply #4 on: May 26, 2020, 01:32:10 PM »
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: [Select]
If(boolean_expression) Then
   Statement n
ElseIf (boolean_expression) Then
   Statement n
ElseIf (boolean_expression) Then
   Statement n
Else
   Statement n
End If

With this methodology:
Code: [Select]
Select Case expression
   Case expressionlistN
      statementN
   Case expressionlistN
      statementN
  Case Else
      elsestatementN
End Select

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"
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

Greg B

  • Seagull
  • Posts: 12354
  • Tell me a Joke!
Re: fix Nested If-Then-Statement
« Reply #5 on: May 27, 2020, 09:09:14 AM »
1.  Been a long time since I did any VB.
2.  Elseif is NOT bad in VB.  Cleans up the code and does the same thing as breaking it all down.
3.  My gut says it is the word HANGER that is causing the issue.  See #1.

Have you tried testing different labels instead of HANGER?

jtm2020hyo

  • Newt
  • Posts: 140
Re: fix Nested If-Then-Statement
« Reply #6 on: May 28, 2020, 01:38:34 AM »
1.  Been a long time since I did any VB.
2.  Elseif is NOT bad in VB.  Cleans up the code and does the same thing as breaking it all down.
3.  My gut says it is the word HANGER that is causing the issue.  See #1.

Have you tried testing different labels instead of HANGER?

I agree, adding a single name not should broken formula... well the problem already was solved.