Question Trying to find a way to streamline this process

jhutchings

Member
Joined
Feb 7, 2014
Messages
11
Programming Experience
1-3
VB.NET:
Imports System.Threading.Thread
Imports System.Threading
Imports System.Threading.ThreadPool
Imports ParseTool.GlobalVariables

Module Main

    Private Delegate Sub IKA()
    Private Delegate Sub Tentative()
    Private Delegate Sub PedDental()
    Private Delegate Sub Pathways()
    Private Delegate Sub VJHold()
    Public loop1 As Boolean = False

    Sub Main()
        Dim dIKA As IKA = New IKA(AddressOf IKAGroupBills.Process)
        Dim dTent As Tentative = New Tentative(AddressOf TentativeLetters.Process)
        Dim dDental As PedDental = New PedDental(AddressOf PediatricDental.Process)
        Dim dPathways As Pathways = New Pathways(AddressOf PathaysQuickStartGuides.Process)
        Dim dVJHold As VJHold = New VJHold(AddressOf VJHoldCode.Process)

        Do Until loop1 = True
            Dim pickUpIKA = Dir("P:\inetpub\ftproot\IH\IKA_BILLING\*.zip")
            Dim pickUpPD = Dir("P:\inetpub\ftproot\IH\Ped Dental Data_Prod\*.xlsx")
            Dim pickUpPQSG = Dir("P:\inetpub\ftproot\IH\QuickStart_Prod\*.txt")
            Dim pickUpTENT = Dir("p:\inetpub\ftproot\as400\archive\tentative letters\tentative*.txt")
            Dim pickUpVJ = Dir("P:\inetpub\wwwroot\PrintTrack\VJHold\*.xlsx")

            Try
                Console.WriteLine("Checking for IKA Group Bills")
                If pickUpIKA <> "" Then
                    Console.WriteLine("Processing IKA Group Bills")
                    dIKA()
                    Console.WriteLine("IKA Group Bills Finished")
                    Console.WriteLine()
                Else
                    Console.WriteLine("No IKA Group Bills")
                    Console.WriteLine()
                End If
            Catch ex As Exception
                ErrorLog.CreateLog("IKA Group Bills", ex)
                Console.WriteLine("Error with IKA Group Bills")
                Console.WriteLine()
            End Try

            Try
                Console.WriteLine("Checking for Pediatric Dental Files")
                If pickUpPD <> "" Then
                    Console.WriteLine("Processing Pediatric Dental Files")
                    dDental()
                    Console.WriteLine("Pediatric Dental Files Finished")
                    Console.WriteLine()
                Else
                    Console.WriteLine("No Pediatric Dental Files")
                    Console.WriteLine()
                End If
            Catch ex As Exception
                ErrorLog.CreateLog("Pediatric Dental", ex)
                Console.WriteLine("Error with Pediatric Dental Files")
                Console.WriteLine()
            End Try

            Try
                Console.WriteLine("Checking for Pathways Quick Start Guides Files")
                If pickUpPQSG <> "" Then
                    Console.WriteLine("Processing Pathways Quick Start Guides Files")
                    dPathways()
                    Console.WriteLine("Pathways Quick Start Guides Files Finished")
                    Console.WriteLine()
                Else
                    Console.WriteLine("No Pathways Quick Start Guide Files")
                    Console.WriteLine()
                End If
            Catch ex As Exception
                ErrorLog.CreateLog("Pathways QSG", ex)
                Console.WriteLine("Error with Pathways Quick Start Guides")
                Console.WriteLine()
            End Try

            Try
                Console.WriteLine("Checking for Tentative Letters")
                If pickUpTENT <> "" Then
                    Console.WriteLine("Processing Tentative Letters")
                    dTent()
                    Console.WriteLine("Tentative Letters Finished")
                    Console.WriteLine()
                Else
                    Console.WriteLine("No Tentative Letters")
                    Console.WriteLine()
                End If
            Catch ex As Exception
                ErrorLog.CreateLog("Tentative Letters", ex)
                Console.WriteLine("Error with Tentative Letters")
                Console.WriteLine()
            End Try

            Try
                Console.WriteLine("Checking for VJ Hold Codes")
                If pickUpVJ <> "" Then
                    Console.WriteLine("Processing VJ Hold Codes")
                    dVJHold()
                    Console.WriteLine("VJ Hold Codes Finished")
                    Console.WriteLine()
                Else
                    Console.WriteLine("No VJ Hold Codes")
                    Console.WriteLine()
                End If
            Catch ex As Exception
                ErrorLog.CreateLog("VJ Hold Codes", ex)
                Console.WriteLine("Error with VJ Hold Codes")
                Console.WriteLine()
            End Try

            Thread.Sleep(50000)
        Loop

    End Sub

End Module

I was wondering if there was a better way to write this code. I am building a parser that finds specific files in different folders and then processes them. However I feel like there is a better way to write this code and I am not sure where to start.

Can anyone help/point me in the right direction.
 
Last edited:
All your Try code blocks are have identical code, so you can make that a method with three parameters.

All your delegates have the same signature and that serves no purpose, use the standard System.Action delegate.
 
All your Try code blocks are have identical code, so you can make that a method with three parameters.

All your delegates have the same signature and that serves no purpose, use the standard System.Action delegate.

VB.NET:
Imports System.Threading.Thread
Imports System.Threading
Imports System.Threading.ThreadPool
Imports ParseTool.GlobalVariables

Module Main

    Public Delegate Sub Processes()
    Public loop1 As Boolean = False

    Sub Main()
        Do Until loop1 = True
            Dim pickUpIKA() = {Dir("P:\inetpub\ftproot\IH\IKA_BILLING\*.zip"), "IKA Group Bills"}
            Dim pickUpPD() = {Dir("P:\inetpub\ftproot\IH\Ped Dental Data_Prod\*.xlsx"), "Pediatic Dental"}
            Dim pickUpPQSG() = {Dir("P:\inetpub\ftproot\IH\QuickStart_Prod\*.txt"), "Pathways Quick Start Guides"}
            Dim pickUpTENT() = {Dir("p:\inetpub\ftproot\as400\archive\tentative letters\tentative*.txt"), "Tentative Letters"}
            Dim pickUpVJ() = {Dir("P:\inetpub\wwwroot\PrintTrack\VJHold\*.xlsx"), "VJ Hold Codes"}

            process(pickUpIKA(0), pickUpIKA(1), AddressOf IKAGroupBills.Process)
            process(pickUpPD(0), pickUpPD(1), AddressOf PediatricDental.Process)
            process(pickUpPQSG(0), pickUpPQSG(1), AddressOf PathwaysQuickStartGuides.Process)
            process(pickUpTENT(0), pickUpTENT(1), AddressOf TentativeLetters.Process)
            process(pickUpVJ(0), pickUpVJ(1), AddressOf VJHoldCode.Process)

            Thread.Sleep(50000)
        Loop

    End Sub

    Public Sub process(ByVal filename As String, ByVal jobname As String, ByVal jobProcess As Processes)
        Try
            If filename <> "" Then
                Console.WriteLine("Processing " & jobname)
                jobProcess()
            Else
                Console.WriteLine("No " & jobname & " files.")
                Console.WriteLine()
            End If
        Catch ex As Exception
            ErrorLog.CreateLog(jobname, ex)
            Console.WriteLine("Error with " & jobname & " check the error logs...")
        End Try
    End Sub

End Module

I was able to shorten it up quite a bit, but I am not quite sure how to do a system.action delegate. I tried researching it before reaching out again and all that I could really find was how to do a system.action delegate using 'list(of)' and I wasn't able to figure out how to apply it. Would you be able to provide a sample of how to complete that task?

Thank you for your help.
 
Last edited:
Action delegate is a delegate defined in .Net library, it is exactly the same as your current Processes delegate (and all the delegates you defined in first post), and can be used instead of that. You can use it as parameter to your Main.process method (like the two String parameters), and pass the relevant 'AddressOf method' as argument. No need for a Select Case in Main.process method, just call Invoke method on the passed delegate instance.
 
Action delegate is a delegate defined in .Net library, it is exactly the same as your current Processes delegate (and all the delegates you defined in first post), and can be used instead of that. You can use it as parameter to your Main.process method (like the two String parameters), and pass the relevant 'AddressOf method' as argument. No need for a Select Case in Main.process method, just call Invoke method on the passed delegate instance.

VB.NET:
Imports System.Threading.Thread
Imports System.Threading
Imports System.Threading.ThreadPool
Imports ParseTool.GlobalVariables

Module Main

    Public Delegate Sub Processes()
    Public loop1 As Boolean = False

    Sub Main()
        Do Until loop1 = True
            Dim pickUpIKA() = {Dir("P:\inetpub\ftproot\IH\IKA_BILLING\*.zip"), "IKA Group Bills"}
            Dim pickUpPD() = {Dir("P:\inetpub\ftproot\IH\Ped [URL="http://www.vbdotnetforums.com/#"]Dental[/URL] Data_Prod\*.xlsx"), "Pediatic Dental"}
            Dim pickUpPQSG() = {Dir("P:\inetpub\ftproot\IH\QuickStart_Prod\*.txt"), "Pathways Quick [URL="http://www.vbdotnetforums.com/#"]Start[/URL] Guides"}
            Dim pickUpTENT() = {Dir("p:\inetpub\ftproot\as400\archive\tentative letters\tentative*.txt"), "Tentative Letters"}
            Dim pickUpVJ() = {Dir("P:\inetpub\wwwroot\PrintTrack\VJHold\*.xlsx"), "VJ Hold Codes"}

            process(pickUpIKA(0), pickUpIKA(1), AddressOf IKAGroupBills.Process)
            process(pickUpPD(0), pickUpPD(1), AddressOf PediatricDental.Process)
            process(pickUpPQSG(0), pickUpPQSG(1), AddressOf PathwaysQuickStartGuides.Process)
            process(pickUpTENT(0), pickUpTENT(1), AddressOf TentativeLetters.Process)
            process(pickUpVJ(0), pickUpVJ(1), AddressOf VJHoldCode.Process)

            Thread.Sleep(50000)
        Loop

    End Sub

    Public Sub process(ByVal filename As String, ByVal jobname As String, ByVal jobProcess As Processes)
        Try
            If filename <> "" Then
                Console.WriteLine("Processing " & jobname)
                jobProcess()
            Else
                Console.WriteLine("No " & jobname & " files.")
                Console.WriteLine()
            End If
        Catch ex As Exception
            ErrorLog.CreateLog(jobname, ex)
            Console.WriteLine("Error with " & jobname & " check the error logs...")
        End Try
    End Sub

End Module

I think I understand, create only one delegate, and pass the (addressof ) as a parameter.

BTW, thank you for your help, it is much appreciated.
 
Here's the Action delegate help: Action Delegate (System)

Next is getting rid of that Dir runtime function and use Directory class "for greater productivity and performance": Directory.GetFiles Method (String, String) (System.IO)

If your processes can be performed simultanously you can also research multi-threading.

Otherwise great effort on refactoring your code! :)

Here's the Action delegate help: Action Delegate (System)

Next is getting rid of that Dir runtime function and use Directory class "for greater productivity and performance": Directory.GetFiles Method (String, String) (System.IO)

If your processes can be performed simultanously you can also research multi-threading.

Otherwise great effort on refactoring your code! :)

VB.NET:
Imports System.Threading
Imports ParseTool.GlobalVariables
Imports System.IO

Module Main

    Public Delegate Sub JobProcess()

    Public Class ProcessName
        Private instanceName As String

        Public Sub New(name As String)
            Me.instanceName = name
        End Sub

        Public Sub JobProcess(ByVal filename As String, ByVal jobProcess As Processes)
            'Public Sub process(ByVal filename As String, ByVal jobname As String, ByVal jobProcess As Processes)
            Console.WriteLine("Checking for files for " & Me.instanceName)
            Console.WriteLine()
            Try
                If filename <> "" Then
                    Console.WriteLine("Processing " & Me.instanceName)
                    JobProcess()
                Else
                    Console.WriteLine("No " & Me.instanceName & " files.")
                    Console.WriteLine()
                End If
            Catch ex As Exception
                ErrorLog.CreateLog(Me.instanceName, ex)
                Console.WriteLine("Error with " & Me.instanceName & " check the error logs...")
            End Try
        End Sub
    End Class

    Sub Main()
        Dim loop1 As Boolean = False
        Do Until loop1 = True
            Dim pickUpIKA As String() = Directory.GetFiles("P:\inetpub\ftproot\IH\IKA_BILLING\", "*.zip")
            Dim pickUpPD As String() = Directory.GetFiles("P:\inetpub\ftproot\IH\Ped Dental Data_Prod\", "*.xlsx")
            Dim pickUpPQSG As String() = Directory.GetFiles("P:\inetpub\ftproot\IH\QuickStart_Prod\", "*.txt")
             Dim pickUpTENT As String() =  Directory.GetFiles("p:\inetpub\ftproot\as400\archive\tentative  letters\", "tentative*.txt")
            Dim pickUpVJ As String() = Directory.GetFiles("P:\inetpub\wwwroot\PrintTrack\VJHold\", "*.xlsx")

            'process(pickUpIKA(0), "IKA Group Bills", AddressOf IKAGroupBills.Process)
            'process(pickUpPD(0), "Pediatic Dental", AddressOf PediatricDental.Process)
            'process(pickUpPQSG(0), "Pathways Quick Start Guides", AddressOf PathwaysQuickStartGuides.Process)
            'process(pickUpTENT(0), "Tentative Letters", AddressOf TentativeLetters.Process)
            'process(pickUpVJ(0), "VJ Hold Codes", AddressOf VJHoldCode.Process)


            Dim ika As New ProcessName("IKA Group Bills")
            Dim methodCall As Action = Sub() ika.JobProcess(pickUpIKA(0), AddressOf IKAGroupBills.Process)
            methodCall()

            Thread.Sleep(50000)
        Loop

    End Sub
End Module

I had the directory.getfiles used throughout my program but I only used DIR because I wasn't sure how to locate files with * otherswise so thank you for that information.

I guess what i am confused on with the action delegate is:

Do i need to create
VB.NET:
Dim ika As New ProcessName("IKA Group Bills")
            Dim methodCall As Action = Sub() ika.JobProcess(pickUpIKA(0), AddressOf IKAGroupBills.Process)
            methodCall()

for each job? or do i set up an if/then structure to pass the job name as
VB.NET:
 Dim ika As New ProcessName("IKA Group Bills")
if the files exists or should i set that up in the PUBLIC SUB JobProcess?
 
I guess what i am confused on with the action delegate is:
It is no different from your own delegate, it just saves you from defining it - why define a delegate when there is already one defined that you can use?
Change JobProcess method "ByVal jobProcess As Processes" to "ByVal jobProcess As Action" and delete your definition for "Public Delegate Sub JobProcess()" and you're done. In Main you just make the call with AddressOf same as in your post 5.
 
It is no different from your own delegate, it just saves you from defining it - why define a delegate when there is already one defined that you can use?
Change JobProcess method "ByVal jobProcess As Processes" to "ByVal jobProcess As Action" and delete your definition for "Public Delegate Sub JobProcess()" and you're done. In Main you just make the call with AddressOf same as in your post 5.

VB.NET:
Imports System.Threading
Imports ParseTool.GlobalVariables
Imports System.IO

Module Main

    Public Class ProcessName
        Private instanceName As String

        Public Sub New(name As String)
            Me.instanceName = name
        End Sub

        Public Sub JobProcess(ByVal jobProcess As Action)
            Try
                Console.WriteLine("Processing " & Me.instanceName)
                jobProcess()
            Catch ex As Exception
                ErrorLog.CreateLog(Me.instanceName, ex)
                Console.WriteLine("Error with " & Me.instanceName & " check the error logs...")
            End Try
        End Sub
    End Class

    Sub Main()
        Dim loop1 As Boolean = False
        Dim methodCall As Action
        Do Until loop1 = True
            Dim pickUpIKA As String() = Directory.GetFiles("P:\inetpub\ftproot\IH\IKA_BILLING\", "*.zip")
            If pickUpIKA.Length = 0 Then
                Console.WriteLine("No IKA Group Bills")
                Console.WriteLine()
            Else
                Dim ika As New ProcessName("IKA Group Bills")
                methodCall = Sub() ika.JobProcess(AddressOf IKAGroupBills.Process)
                methodCall()
            End If

            Dim pickUpPD As String() = Directory.GetFiles("P:\inetpub\ftproot\IH\Ped Dental Data_Prod\", "*.xlsx")
            If pickUpPD.Length = 0 Then
                Console.WriteLine("No Pediatric Dental Files")
                Console.WriteLine()
            Else
                Dim pd As New ProcessName("Pediatric Dental")
                methodCall = Sub() pd.JobProcess(AddressOf PediatricDental.Process)
                methodCall()
            End If
            
            Dim pickUpPQSG As String() = Directory.GetFiles("P:\inetpub\ftproot\IH\QuickStart_Prod\", "*.txt")
            If pickUpPQSG.Length = 0 Then
                Console.WriteLine("No Pathways Quick Start Guides")
                Console.WriteLine()
            Else
                Dim pqsg As New ProcessName("Pathways Quick Start Guides")
                methodCall = Sub() pqsg.JobProcess(AddressOf PathwaysQuickStartGuides.Process)
                methodCall()
            End If

            Dim pickUpTENT As String() = Directory.GetFiles("p:\inetpub\ftproot\as400\archive\tentative  letters\", "tentative*.txt")
            If pickUpTENT.Length = 0 Then
                Console.WriteLine("No Tentative Letters")
                Console.WriteLine()
            Else
                Dim tent As New ProcessName("Tentative Letters")
                methodCall = Sub() tent.JobProcess(AddressOf TentativeLetters.Process)
                methodCall()
            End If

            Dim pickUpVJ As String() = Directory.GetFiles("P:\inetpub\wwwroot\PrintTrack\VJHold\", "*.xlsx")
            If pickUpVJ.Length = 0 Then
                Console.WriteLine("No VJ Hold Code Files")
                Console.WriteLine()
            Else
                Dim vj As New ProcessName("VJ Hold Codes")
                methodCall = Sub() vj.JobProcess(AddressOf VJHoldCode.Process)
                methodCall()
            End If

            Thread.Sleep(50000)
        Loop

    End Sub
End Module

Thank you again for all of your help!
 
VB.NET:
Imports System.Threading
Imports System.IO
Imports ParseTool.GlobalVariables

Module Main

    Public loop1 As Boolean = False

    Sub Main()
        Do Until loop1 = True
            Dim pickUpIKA As String() = Directory.GetFiles("P:\inetpub\ftproot\IH\IKA_BILLING\", "*.zip")
            Dim pickUpPD As String() = Directory.GetFiles("P:\inetpub\ftproot\IH\Ped Dental Data_Prod\", "*.xlsx")
            Dim pickUpPQSG As String() = Directory.GetFiles("P:\inetpub\ftproot\IH\QuickStart_Prod\", "*.txt")
            Dim pickUpTENT As String() = Directory.GetFiles("p:\inetpub\ftproot\as400\archive\tentative  letters\", "tentative*.txt")
            Dim pickUpVJ As String() = Directory.GetFiles("P:\inetpub\wwwroot\PrintTrack\VJHold\", "*.xlsx")

            process(pickUpIKA.Length, "IKA Group Bills", AddressOf IKAGroupBills.Process)
            process(pickUpPD.Length, "Pediatric Dental", AddressOf PediatricDental.Process)
            process(pickUpPQSG.Length, "Pathways Quick Start Guides", AddressOf PathwaysQuickStartGuides.Process)
            process(pickUpTENT.Length, "Tentative Letters", AddressOf TentativeLetters.Process)
            process(pickUpVJ.Length, "VJ Hold Codes", AddressOf VJHoldCode.Process)

            Thread.Sleep(50000)
        Loop

    End Sub

    Public Sub process(ByVal filename As Integer, ByVal jobname As String, ByVal jobProcess As Action)
        Try
            If filename <> 0 Then
                Console.WriteLine("Processing " & jobname)
                jobProcess()
            Else
                Console.WriteLine("No " & jobname & " files.")
                Console.WriteLine()
            End If
        Catch ex As Exception
            ErrorLog.CreateLog(jobname, ex)
            Console.WriteLine("Error with " & jobname & " check the error logs...")
        End Try
    End Sub

End Module

here is my corrected code, I think it should be all set! Thank you so much... I have another question regarding importing/exporting data from excel to .txt.. should I start a new thread for that?
 
I have another question regarding importing/exporting
Yes, you should. One topic each thread.
 
Back
Top