Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how I can optimize this code? if else

Posted on 2011-03-23
6
Medium Priority
?
447 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:enrique_aeo
  • 3
  • 2
6 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35200026
>>  how I can optimize this code

Define "optimize".
0
 
LVL 13

Accepted Solution

by:
Rick earned 1000 total points
ID: 35200037
You could use SELECT CASE instead of IF THEN statements, and replace these lines:

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

With these:

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


And replace these:

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

With these:

With WFGDH.UI.Session
   .varCodVicepresidencia = Nothing
   .varCodGCentral = Nothing
   .varCodDivision = Nothing
   .varCodDepartamento = Nothing
End With
0
 
LVL 13

Expert Comment

by:Rick
ID: 35200046
But that will in no way "optimize" the existing code.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:enrique_aeo
ID: 35200196
reduce the number of lines of code
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35201137
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

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35201169
More of a horizontal savings rather than a vertical one  ; )
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question