"On error resume next" vs. "for each"-loop

"On error resume next" vs. "for each"-loop

mr_ensing
Advocate Advocate
2,056 Views
2 Replies
Message 1 of 3

"On error resume next" vs. "for each"-loop

mr_ensing
Advocate
Advocate

First of all,  there is not really a problem to solve. It's more a question of what is best practice.

 

I wrote the below function to check for a specific parameter and add it if it's missing.

At first i use the for each loop to find the parameters (now commented out).

But i later realized i could just assign the parameters, while suppressing errors. Because later in the routine i check if the parameters exist anyway.

 

My question is, what would be the best practice in this case? The loop or the error suppressing? And why?

 

Sub TestThicknessInch()
    If ThisApplication.ActiveDocumentType <> kPartDocumentObject Then Exit Sub
    Dim oPart As PartDocument
    Set oPart = ThisApplication.ActiveDocument
    Call AddThicknessInch(oPart.ComponentDefinition.Parameters)
End Sub

Private Function AddThicknessInch(ByRef oParas As Parameters)
    Dim Thickness As Parameter
    Dim ThicknessInch As Parameter

    ' Dim oPara As Parameter
    ' For Each oPara In oParas
    '     If oPara.Name = "Thickness_Inch" Then Set ThicknessInch = oPara
    '     If oPara.Name = "Thickness" Then Set Thickness = oPara
    ' Next oPara

    On Error Resume Next
        Set Thickness = oParas.Item("Thickness")
        Set ThicknessInch = oParas.Item("Thickness_Inch")
    On Error GoTo 0

    If Thickness Is Nothing Then Exit Function
    
    If ThicknessInch Is Nothing Then
        Call oParas.UserParameters.AddByExpression("Thickness_Inch", "Thickness", "ft")
        Set ThicknessInch = oParas.Item("Thickness_Inch")
    End If
    
    If ThicknessInch.Units <> "ft" Then ThicknessInch.Units = "ft"
    If ThicknessInch.DisplayFormat <> kDecimalDisplayFormat Then _
            ThicknessInch.DisplayFormat = kFractionalDisplayFormat
    If ThicknessInch.ExposedAsProperty = False Then _
            ThicknessInch.ExposedAsProperty = True
    If Not ThicknessInch.CustomPropertyFormat.Precision = kSixtyFourthsFractionalLengthPrecision Then _
            ThicknessInch.CustomPropertyFormat.Precision = kSixtyFourthsFractionalLengthPrecision
End Function

 

2,057 Views
2 Replies
Replies (2)
Message 2 of 3

Darkforce_the_ilogic_guy
Advisor
Advisor

I think it depend on what you want.  I use try and  catch to handle ERROR must of the time.

 

I think what you want it is the one that make your system runs as lilted code as possible.. and of cause how most custom control you would have over what happen next...

 

loop is for a code write  saving And/or for code flexible.. and I do not think it do most good for performers try and  catch is too for tell you program to do something special Error messing or stop the code

 

don't think  suppressing errors would bee very good for more complex system where the same code will be run on 1000´s of files... with is want I work on ...

Message 3 of 3

mr_ensing
Advocate
Advocate

I forgot to mention this is VBA code, so Try...Catch is not available. If it was it probably would be best used here, I agree.

0 Likes