iNET Interactive - Online Advertising Agency
          
Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > A little optimization puzzle


Reply
 
Thread Tools Display Modes
  #1  
Old 10-12-2009, 08:22 PM
williamlove williamlove is offline
Regular
 
Join Date: Sep 2003
Posts: 59
Question A little optimization puzzle

I think some of you may have learned how to make the following nested If more compact, maybe in an algorithms discussion in school or some such place. If so I'd like to learn the trick. It is an example of a situation I run into occasionally. Or, maybe the code cannot be further reduced and I'm looking for something needlessly. (The code works fine as is.) I had one person give me an incorrect reduction but I'm still curious to see if anyone can teach me something.

bFileMissing = False ' Initialize
If (CantFindFile(sName)) Then
____If bCheckOtherPlace Then
________Call SwitchLocation(sName)
________If (CantFindFile(sName)) Then
____________bFileMissing = False
________Else
____________bFileMissing = True
________End If
____Else
________bFileMissing = True
____End If
Else
____bFileMissing = False
End If

If bFileMissing Then MsgBox "Not Found"

(by the way, how do you overcome the whitespace eliminator and show indentation in examples without doing what I just did?)

Last edited by williamlove; 10-12-2009 at 10:18 PM.
Reply With Quote
  #2  
Old 10-12-2009, 10:19 PM
vb5prgrmr vb5prgrmr is offline
Contributor
 
Join Date: Mar 2009
Posts: 694
Default

[code ] [ /code] tags (no spaces between [ and first character)

Code:
If notfound = true AND SearchOther = True Then
  SwitchLocation
  If NotFound = False Then found = True
End If

If Found = False Then MsgBox "Not Found"
BTW...
Code:
Dim Found As Boolean 'automatically equals false
Code:
Private Sub SomeSub()
Dim Result As Boolean '=false
Result = SomeFunction 'still = false
End Sub

Private Function SomeFunction()
DoEvents
End Function
Boolean variables and Functions that return boolean values are automatically equalling false unless you set them to a true value in some way (= True, = -1)



Good Luck
Reply With Quote
  #3  
Old 10-13-2009, 04:59 PM
williamlove williamlove is offline
Regular
 
Join Date: Sep 2003
Posts: 59
Smile error in my example

Thanks, I will consider your solution further. I noticed I made an error in my code example, which was a simplified version of my actual code.

I wrote
Code:
If (CantFindFile(sName)) Then
    bFileMissing = False
Else
    bFileMissing = True
End If

which should have been

If (CantFindFile(sName)) Then
    bFileMissing = True
Else
    bFileMissing = False
End If
The actual code I’m trying to reduce is this (Me is an Access form):

Code:
'Now look for a folder containing the ID.
sSearchPath = sCategoryPath & "\*" & Me.txtID & "*"
If Dir(sSearchPath, vbDirectory) = "" Then
    ' We didn't find it. If both Current and MP were checked, then look for it in the other directory.
    ' Note that another routine will inform the user about this situation.
    If bErrorCurrentAndMPBothChecked Then
        If sCategoryPath = sCategoryPath = m_RootFolderPath & "\" & "Current" Then
            sCategoryPath = m_RootFolderPath & "\" & "MeaningfulPast"
            sCategory = "MeaningfulPast"
        Else
            sCategoryPath = sCategoryPath = m_RootFolderPath & "\" & "Current"
            sCategory = "Current"
        End If
        ' Now that we switched, try again...
        sSearchPath = sCategoryPath & "\*" & Me.txtID & "*"
        If Dir(sSearchPath, vbDirectory) = "" Then
            bFolderNotFound = True
        Else
            ' We found it in the other folder.
            bFolderNotFound = False
            sProfileFolderName = Dir(sSearchPath, vbDirectory)
        End If
    Else
        bFolderNotFound = True
    End If
Else
    bFolderNotFound = False
    sProfileFolderName = Dir(sSearchPath, vbDirectory)
End If
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement: