Link to home
Start Free TrialLog in
Avatar of enrique_aeo
enrique_aeo

asked on

how I can optimize this code? if else

hi experts, how I can optimize this code?
 
   If WFGDH.UI.Session.desicionSolicitud <> "NO" Then
                If WFGDH.UI.Session.varCodVicepresidencia.Equals("") Or WFGDH.UI.Session.varCodGCentral.Equals("") Or _
                        WFGDH.UI.Session.varCodDivision.Equals("") Or WFGDH.UI.Session.varCodDepartamento.Equals("") Then
                    btnNuevoColaborador.Attributes.Add("onclick", "javascript:return pCargarModal('Ascenso','N','-1','NN');")
                ElseIf (WFGDH.UI.Session.varCodVicepresidencia.Equals(WFGDH.UI.Session.varCodDepartamento)) Then
                    btnNuevoColaborador.Attributes.Add("onclick", "javascript:return pCargarModal('Ascenso','N','-1','Vp');")
                ElseIf (WFGDH.UI.Session.varCodGCentral.Equals(WFGDH.UI.Session.varCodDepartamento)) Then
                    btnNuevoColaborador.Attributes.Add("onclick", "javascript:return pCargarModal('Ascenso','N','-1','Ce');")
                ElseIf (WFGDH.UI.Session.varCodDivision.Equals(WFGDH.UI.Session.varCodDepartamento)) Then
                    btnNuevoColaborador.Attributes.Add("onclick", "javascript:return pCargarModal('Ascenso','N','-1','Dv');")
                Else
                    btnNuevoColaborador.Attributes.Add("onclick", "javascript:return pCargarModal('Ascenso','N','-1','Dp');")
                End If
            Else

                WFGDH.UI.Session.varCodVicepresidencia = WFGDH.UI.Session.SelUO
                WFGDH.UI.Session.varCodGCentral = WFGDH.UI.Session.SelUO
                WFGDH.UI.Session.varCodDivision = WFGDH.UI.Session.SelUO
                WFGDH.UI.Session.varCodDepartamento = WFGDH.UI.Session.SelUO

                If WFGDH.UI.Session.varCodVicepresidencia.Equals("") Or WFGDH.UI.Session.varCodGCentral.Equals("") Or _
                        WFGDH.UI.Session.varCodDivision.Equals("") Or WFGDH.UI.Session.varCodDepartamento.Equals("") Then
                    btnNuevoColaborador.Attributes.Add("onclick", "javascript:return pCargarModal('Ascenso','N','-1','NN');")
                ElseIf (WFGDH.UI.Session.varCodVicepresidencia.Equals(WFGDH.UI.Session.varCodDepartamento)) Then
                    btnNuevoColaborador.Attributes.Add("onclick", "javascript:return pCargarModal('Ascenso','N','-1','Vp');")
                ElseIf (WFGDH.UI.Session.varCodGCentral.Equals(WFGDH.UI.Session.varCodDepartamento)) Then
                    btnNuevoColaborador.Attributes.Add("onclick", "javascript:return pCargarModal('Ascenso','N','-1','Ce');")
                ElseIf (WFGDH.UI.Session.varCodDivision.Equals(WFGDH.UI.Session.varCodDepartamento)) Then
                    btnNuevoColaborador.Attributes.Add("onclick", "javascript:return pCargarModal('Ascenso','N','-1','Dv');")
                Else
                    btnNuevoColaborador.Attributes.Add("onclick", "javascript:return pCargarModal('Ascenso','N','-1','Dp');")
                End If

                WFGDH.UI.Session.varCodVicepresidencia = Nothing
                WFGDH.UI.Session.varCodGCentral = Nothing
                WFGDH.UI.Session.varCodDivision = Nothing
                WFGDH.UI.Session.varCodDepartamento = Nothing
            End If
Avatar of kaufmed
kaufmed
Flag of United States of America image

>>  how I can optimize this code

Define "optimize".
ASKER CERTIFIED SOLUTION
Avatar of Rick
Rick

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rick
Rick

But that will in no way "optimize" the existing code.
Avatar of enrique_aeo

ASKER

reduce the number of lines of code
Untested, but I believe you could do something along these lines (as rick_gwu indicated):
With WFGDH.UI.Session
    Dim option As String
    
    If .desicionSolicitud <> "NO" Then
        .varCodVicepresidencia = .SelUO
        .varCodGCentral = .SelUO
        .varCodDivision = .SelUO
        .varCodDepartamento = .SelUO
    End If

    Select Case True
        Case .varCodVicepresidencia.Equals(String.Empty), .varCodGCentral.Equals(String.Empty), _
             .varCodDivision.Equals(String.Empty), .varCodDepartamento.Equals(String.Empty)
            option = "NN"
        Case .varCodVicepresidencia.Equals(.varCodDepartamento)
            option = "Vp"
        Case.varCodGCentral.Equals(.varCodDepartamento)
            option = "Ce"
        Case .varCodDivision.Equals(.varCodDepartamento)
            option = "Dv"
        Case Else
            option = "Dp"
    End Select
    
    btnNuevoColaborador.Attributes.Add("onclick", String.Format("javascript:return pCargarModal('Ascenso','N','-1','{0}');", option))
    
    If .desicionSolicitud = "NO" Then
        .varCodVicepresidencia = Nothing
        .varCodGCentral = Nothing
        .varCodDivision = Nothing
        .varCodDepartamento = Nothing
    End If
End With

Open in new window

More of a horizontal savings rather than a vertical one  ; )