Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change If-Then-Else to Select Case

Status
Not open for further replies.

mcelligott

Programmer
Apr 17, 2002
135
US
Hello All,

I have the following code:

Code:
If FSO.FileExists(strServerPath) = True Then
    Call pfECDServerReLink
End If
If FSO.FileExists(strServerPath) = False Then
    If FSO.FileExists(strMCUPath) = True Then
        Call pfMCUReLink
    End If
    If FSO.FileExists(strMCUPath) = False Then
        If FSO.FileExists(strAltServerPath) = True Then
            Call pfAltServerReLink
        End If
        If FSO.FileExists(strAltServerPath) = False Then
            If FSO.FileExists(strVPNPath) = True Then
                Call pfVPNRelink
            End If
            If FSO.FileExists(strVPNPath) = False Then
                If FSO.FileExists(strGFDPath) = True Then
                    Call pfGFDReLink
                End If
                If FSO.FileExists(strGFDPath) = False Then
                    If FSO.FileExists(strPerintonPath) = True Then
                       Call pfPerintonReLink
                    End If
                    If FSO.FileExists(strPerintonPath) = False Then
                        Call pfLocalReLink
                    End If
                End If
            End If
        End If
    End If
End If

I would like to re-write it using a Select Case statement because I think it would be more efficient. I am having difficulty getting my brain to come up with the correct way to do it because of the variable in the parenthesis.

Any thoughts?

Bob
 


hi,

not a select case instance AFAIK, but maybe...
Code:
    Dim sPath() As String, i As Index

'assign the path string to sPath array
'...???? from a table???
    
'figure out which one
    For i = 0 To UBound(sPath)
        If fso.FileExists(sPath(i)) Then
            Run "pf" & sPath(i) 'or something like this
        End If
    Next

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Why not just correctly structure your if then?
Code:
If FSO.FileExists(strServerPath) Then
    Call pfECDServerReLink
else
   If FSO.FileExists(strMCUPath) Then
      Call pfMCUReLink
   else
     If FSO.FileExists(strAltServerPath) Then
        Call pfAltServerReLink
     else
        If FSO.FileExists(strVPNPath) Then
           Call pfVPNRelink
        else
           If FSO.FileExists(strGFDPath) Then
              Call pfGFDReLink
           else
             If FSO.FileExists(strPerintonPath) Then
               call pfPerintonReLink
             else
               Call pfLocalReLink
             End If
           End If
        End If
     End If
   End If
End If
 
Also this is incorrect
Select Case statement because I think it would be more efficient.

A select case is no more efficient than a properly structured If then. Select Case may is easier to read and construct when you have a lot of cases. When you have a lot of nested conditions, I find if then easier. You could do the above in a select case, but it would not be any easier to construct or read.
 

Or a little shorter version of your code:
Code:
If FSO.FileExists(strServerPath) = True Then
    Call pfECDServerReLink
Else
    If FSO.FileExists(strMCUPath) = True Then
        Call pfMCUReLink
    Else
        If FSO.FileExists(strAltServerPath) = True Then
            Call pfAltServerReLink
        Else
            If FSO.FileExists(strVPNPath) = True Then
                Call pfVPNRelink
            Else
                If FSO.FileExists(strGFDPath) = True Then
                    Call pfGFDReLink
                Else
                    If FSO.FileExists(strPerintonPath) = True Then
                       Call pfPerintonReLink
                    Else
                        Call pfLocalReLink
                    End If
                End If
            End If
        End If
    End If
End If

Have fun.

---- Andy
 
How are ya mcelligott . . .

What I see is a hierarchal execution of subroutines dependent on wether a file exists (True). Also ... the first file found executes that subroutine and ends the [blue]If Statement[/blue]. If no files are found [blue]pfLocalReLink[/blue] is always ececuted. You've simply complicated your code by pinging on (False) for the same file. Removing these (False) comparisons results in the following hierarchy:
Code:
[blue]   If FSO.FileExists(strServerPath) = True Then
      Call pfECDServerReLink
   ElseIf FSO.FileExists(strMCUPath) = True Then
      Call pfMCUReLink
   ElseIf FSO.FileExists(strAltServerPath) = True Then
      Call pfAltServerReLink
   ElseIf FSO.FileExists(strVPNPath) = True Then
      Call pfVPNRelink
   ElseIf FSO.FileExists(strGFDPath) = True Then
      Call pfGFDReLink
   ElseIf FSO.FileExists(strPerintonPath) = True Then
      Call pfPerintonReLink
   Else
      Call pfLocalReLink
   End If[/blue]
[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
not sure why everyone seems hung up on the
If true = true then
construct. Versus the
if true then

IMHO this is redundant
If FSO.FileExists(strServerPath) = True Then

Simply
If FSO.FileExists(strServerPath)Then
 


Its a TRUE FACT! ;-)

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 

If you have your heart set to Select Case, you may follow TheAceMan1's code and do:
Code:
Select Case True
    Case FSO.FileExists(strServerPath)
        Call pfECDServerReLink
    Case FSO.FileExists(strMCUPath)
        Call pfMCUReLink
    Case FSO.FileExists(strAltServerPath) 
        Call pfAltServerReLink
    Case FSO.FileExists(strVPNPath) 
        Call pfVPNRelink
    Case FSO.FileExists(strGFDPath) 
        Call pfGFDReLink
    Case FSO.FileExists(strPerintonPath)
        Call pfPerintonReLink
    CaseElse
        Call pfLocalReLink
End Select

PS. I did not copy MajP's code, I just was to busy to finish it in time :)

Have fun.

---- Andy
 
Thank you all for your responses. They were all helpful.
 
How are ya [blue]MajP[/blue] . . .

Referring to [blue]22 Apr 10 7:22[/blue] ... I simply copied & restructured the code. Although I'm aware of its redundancy, as a matter of posting back faster, I decided to leave it in its [blue]explicit[/blue] form. As programmers, we enough times get a [blue]little too picky ourselves ...[/blue]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top