[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. :o 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:
Private Sub bwApp_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bwApp.DoWork
Dim t As New cBwJob(cBwJob.JobType.NEW_TASK)
t = CType(e.Argument, cBwJob)
Select Case t.Job
Case cBwJob.JobType.ADD_NEW_TRACKS
sBwAppAddNewFilesToLibrary(t.TaskData)
Case cBwJob.JobType.CAPITALIZE_FIRST_LETTER
sBwAppCaptitalizeWord()
Case cBwJob.JobType.FIND_NEW_TRACKS_FROM_HDD
sBwAppFindNewTracksFromHDD()
Case cBwJob.JobType.INITIALIZE_ITUNES
sBwAppLoadItunes()
Case cBwJob.JobType.LOAD_ALBUMS
sBwAppFillTrackCount()
Case cBwJob.JobType.RATINGS_BACKUP
sBwAppRatingsBackup()
Case cBwJob.JobType.RATINGS_RESTORE
sBwAppRatingsRestore()
Case cBwJob.JobType.RECOVER_TAGS
sBwAppRecoverTags()
Case cBwJob.JobType.REPLACE_WORD
sBwAppReplaceTextInTags()
Case cBwJob.JobType.VALIDATE_LIBRARY
If mAppInfo.ApplicationState = AppInfo.SoftwareCycle.FINAL Then
sBwAppValidateLibrary()
Else
Try
sBwAppValidateLibrary()
Catch ex As Exception
mErrOccured = True
Dim lErrLog As New System.IO.StreamWriter(mFilePathErrorLog)
lErrLog.WriteLine(String.Format("{0}", ex.Message))
lErrLog.WriteLine(String.Format("{0}", ex.StackTrace))
lErrLog.Close()
End Try
End If
End Select
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:
Private Sub sPerformTask(Function myFunction)
If mAppInfo.ApplicationState = AppInfo.SoftwareCycle.FINAL Then
Try
Call myFunction()
Catch ex As Exception
mErrOccured = True
Dim lErrLog As New System.IO.StreamWriter(mFilePathErrorLog)
lErrLog.WriteLine(String.Format("{0}", ex.Message))
lErrLog.WriteLine(String.Format("{0}", ex.StackTrace))
lErrLog.Close()
End Try
Else
Call myFunction()
End If
End Sub
So I can call this like
VB Code:
sPerformTask(sBwAppReplaceTextInTags())
VB Code:
sPerformTask(sBwAppReplaceTextInTags())
VB Code:
sPerformTask(sBwAppReplaceTextInTags())
Etc...
I hope I am making myself clear.
Any help would be much appreciated.
Thanks,
McoreD
Re: [2005] Pass a Function or Sub as a Parameter?
:mad: 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.
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:
Private Sub sExecuteJob(ByVal mySub As MethodInvoker)
If mAppInfo.ApplicationState = AppInfo.SoftwareCycle.FINAL Then
Try
mySub.Invoke()
Catch ex As Exception
mErrOccured = True
Dim lErrLog As New System.IO.StreamWriter(mFilePathErrorLog)
lErrLog.WriteLine(String.Format("{0}", ex.Message))
lErrLog.WriteLine(String.Format("{0}", ex.StackTrace))
lErrLog.Close()
End Try
Else
mySub.Invoke()
End If
End Sub
That enabled me creating my ideal function.