Results 1 to 3 of 3

Thread: [RESOLVED] [2005] Pass a Function or Sub as a Parameter?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Australia
    Posts
    252

    Resolved [RESOLVED] [2005] Pass a Function or Sub as a Parameter?

    Hi All,

    I am asking too many questions too often and it looks like I am not doing my research but I really do. It's just that I code and learn during weekends and all questions arise in a short interval.

    For a while I was looking for a way to Pass a Function or Sub as a Parameter, if this is what you were trying to find rickford?

    Here is my problem. I have many functions (Background Worker tasks) that I like to do in a Try/Catch block for FINAL versions and without Try/Catch block for BETA versions.

    VB Code:
    1. Private Sub bwApp_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bwApp.DoWork
    2.  
    3.         Dim t As New cBwJob(cBwJob.JobType.NEW_TASK)
    4.         t = CType(e.Argument, cBwJob)
    5.  
    6.         Select Case t.Job
    7.  
    8.             Case cBwJob.JobType.ADD_NEW_TRACKS
    9.                 sBwAppAddNewFilesToLibrary(t.TaskData)
    10.  
    11.             Case cBwJob.JobType.CAPITALIZE_FIRST_LETTER
    12.                 sBwAppCaptitalizeWord()
    13.  
    14.             Case cBwJob.JobType.FIND_NEW_TRACKS_FROM_HDD
    15.                 sBwAppFindNewTracksFromHDD()
    16.  
    17.             Case cBwJob.JobType.INITIALIZE_ITUNES
    18.                 sBwAppLoadItunes()
    19.  
    20.             Case cBwJob.JobType.LOAD_ALBUMS
    21.                 sBwAppFillTrackCount()
    22.  
    23.             Case cBwJob.JobType.RATINGS_BACKUP
    24.                 sBwAppRatingsBackup()
    25.  
    26.             Case cBwJob.JobType.RATINGS_RESTORE
    27.                 sBwAppRatingsRestore()
    28.  
    29.             Case cBwJob.JobType.RECOVER_TAGS
    30.                 sBwAppRecoverTags()
    31.  
    32.             Case cBwJob.JobType.REPLACE_WORD
    33.                 sBwAppReplaceTextInTags()
    34.  
    35.             Case cBwJob.JobType.VALIDATE_LIBRARY
    36.  
    37.                 If mAppInfo.ApplicationState = AppInfo.SoftwareCycle.FINAL Then
    38.                     sBwAppValidateLibrary()
    39.                 Else
    40.                     Try
    41.                         sBwAppValidateLibrary()
    42.                     Catch ex As Exception
    43.                         mErrOccured = True
    44.                         Dim lErrLog As New System.IO.StreamWriter(mFilePathErrorLog)
    45.                         lErrLog.WriteLine(String.Format("{0}", ex.Message))
    46.                         lErrLog.WriteLine(String.Format("{0}", ex.StackTrace))
    47.                         lErrLog.Close()
    48.                     End Try
    49.                 End If
    50.  
    51.         End Select
    52.  
    53.     End Sub

    If you look at the code above, I have done this for ONE BackgroundWorder tasks and I would like to extend this to all the other tasks. I could do it, but ideally I was hoping for something like this to do:


    VB Code:
    1. Private Sub sPerformTask(Function myFunction)
    2.  
    3.      If mAppInfo.ApplicationState = AppInfo.SoftwareCycle.FINAL Then
    4.  
    5.      Try
    6.              Call myFunction()
    7.          Catch ex As Exception
    8.              mErrOccured = True
    9.              Dim lErrLog As New System.IO.StreamWriter(mFilePathErrorLog)
    10.              lErrLog.WriteLine(String.Format("{0}", ex.Message))
    11.              lErrLog.WriteLine(String.Format("{0}", ex.StackTrace))
    12.              lErrLog.Close()
    13.          End Try
    14.  
    15.      Else
    16.        
    17.      Call myFunction()
    18.        
    19.      End If
    20.  
    21. End Sub

    So I can call this like

    VB Code:
    1. sPerformTask(sBwAppReplaceTextInTags())
    VB Code:
    1. sPerformTask(sBwAppReplaceTextInTags())
    VB Code:
    1. sPerformTask(sBwAppReplaceTextInTags())

    Etc...

    I hope I am making myself clear.

    Any help would be much appreciated.

    Thanks,
    McoreD

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Pass a Function or Sub as a Parameter?

    Please don't duplicate post. I just finished answering this question in another thread and here it is again. That's a great way to confuse the issue and waste people's time when they post answers that have already been posted in another thread.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Australia
    Posts
    252

    Re: [RESOLVED] [2005] Pass a Function or Sub as a Parameter?

    jmcilhinney, you were too quick. I just finished reading rick's post in the other thread and was just following a suggestion because I thought I was stealing his thread. As soon as I saw you posted in the other thread I was going to make this Thread as Resolved.

    http://www.vbforums.com/showpost.php...21&postcount=5

    Thanks very much for the idea of MethodInvoker.

    VB Code:
    1. Private Sub sExecuteJob(ByVal mySub As MethodInvoker)
    2.  
    3.         If mAppInfo.ApplicationState = AppInfo.SoftwareCycle.FINAL Then
    4.             Try
    5.                 mySub.Invoke()
    6.             Catch ex As Exception
    7.                 mErrOccured = True
    8.                 Dim lErrLog As New System.IO.StreamWriter(mFilePathErrorLog)
    9.                 lErrLog.WriteLine(String.Format("{0}", ex.Message))
    10.                 lErrLog.WriteLine(String.Format("{0}", ex.StackTrace))
    11.                 lErrLog.Close()
    12.             End Try
    13.         Else
    14.             mySub.Invoke()
    15.         End If
    16.  
    17.     End Sub

    That enabled me creating my ideal function.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width