How to simplify similar code blocks in one thread in vb.net

Hi,

I'm using below code for running some procedures and also i would like to show them their status in my data grid view as below.

But i'd like to know that is there any way to simplify these code or you think it's the best approach i can?

Especially i mean this type of blocks(i take one of them as an example from bigger code block)

          If nodeForceOut.Checked = True Then
                Dim rowIndex As Integer = FindRowDgv(nodeForceOut.Text)
                Try
                    StyleStatus(rowIndex, "Performing", Color.Yellow)
                    ForceOut.MeasureForceMarker()
                    ForceOut.ListForceOutputs()
                    StyleStatus(rowIndex, "Finished", Color.Green)
                Catch ex As Exception
                    StyleStatus(rowIndex, "Error Occured", Color.Red)
                    Tools.OutputLog.Print(Tools.OutputLog.Type.ERR, ex.Message)
                End Try
            End If

Open in new window


Any help would be grateful. Thank you.

        Public Shared Sub Run(ByVal mdl As IScrModel)
            If mdl Is Nothing Then
                Throw New ArgumentNullException(NameOf(mdl))
            End If

            Dim tView As TreeView = GUI.treCheckListMain
            Dim dgView As DataGridView = GUI.dgvCheckInfo
            dgView.Rows.Clear()

            For Each nodeParent As TreeNode In tView.Nodes
                If nodeParent.Checked Then
                    dgView.Rows.Add(nodeParent.Text, "In queue")
                    Dim tpName As String = "tpCheck" & nodeParent.Tag.ToString
                    Dim tcName As String = "tcCheck" & nodeParent.Tag.ToString
                    If CheckTabPage(GUI.tabCheckLocal, tpName) = False Then
                        Dim tabPage As New TabPage With {
                            .Name = tpName,
                            .Text = nodeParent.Text
                        }
                        Dim tabControl As New TabControl With {
                        .Name = tcName,
                        .Dock = DockStyle.Fill
                        }
                        tabPage.Controls.Add(tabControl)
                        GUI.tabCheckLocal.TabPages.Add(tabPage)
                    End If

                End If
            Next

            Dim nodeBody As TreeNode = tView.Nodes.Item("nodBody")
            Dim nodeMarker As TreeNode = tView.Nodes.Item("nodMarker")
            Dim nodeSubvar As TreeNode = tView.Nodes.Item("nodSubvar")
            Dim nodeForce As TreeNode = tView.Nodes.Item("nodForce")
            Dim nodeForceOut As TreeNode = tView.Nodes.Item("nodForceOut")
            Dim nodeIfctn As TreeNode = tView.Nodes.Item("nodIfctn")
            Dim nodeComm As TreeNode = tView.Nodes.Item("nodComm")
            Dim nodeExpression As TreeNode = tView.Nodes.Item("nodExpression")
            Dim nodeSolve As TreeNode = tView.Nodes.Item("nodSolve")
            Dim nodeSubstr As TreeNode = tView.Nodes.Item("nodSubstr")
            Dim nodeNaming As TreeNode = tView.Nodes.Item("nodNaming")


            If nodeBody.Checked <> True Then
            Else
                Dim rowIndex As Integer = FindRowDgv(nodeBody.Text)
                Try
                    StyleStatus(rowIndex, "Performing", Color.Yellow)
                    Body.ListBodies()
                    StyleStatus(rowIndex, "Finished", Color.Green)
                Catch ex As Exception
                    StyleStatus(rowIndex, "Error Occured", Color.Red)
                    Tools.OutputLog.Print(Tools.OutputLog.Type.ERR, ex.Message)
                End Try
            End If

            If nodeMarker.Checked = True Then
                Dim rowIndex As Integer = FindRowDgv(nodeMarker.Text)
                Try
                    StyleStatus(rowIndex, "Performing", Color.Yellow)
                    Marker.ListMarker()
                    Marker.ListMarkersBRF()
                    StyleStatus(rowIndex, "Finished", Color.Green)
                Catch ex As Exception
                    StyleStatus(rowIndex, "Error Occured", Color.Red)
                    Tools.OutputLog.Print(Tools.OutputLog.Type.ERR, ex.Message)
                End Try
            End If

            If nodeSubvar.Checked = True Then
                Dim rowIndex As Integer = FindRowDgv(nodeSubvar.Text)
                Try
                    StyleStatus(rowIndex, "Performing", Color.Yellow)
                    Subvar.ListSubvarPa()
                    Subvar.ListSubvarHp()
                    Subvar.ListSubvarsSensor()
                    Subvar.ListSubvarMp()
                    Subvar.ListSubvarFe()
                    Subvar.ListSubvar3d()
                    Subvar.ListSubvarCom()
                    StyleStatus(rowIndex, "Finished", Color.Green)
                Catch ex As Exception
                    StyleStatus(rowIndex, "Error Occured", Color.Red)
                    Tools.OutputLog.Print(Tools.OutputLog.Type.ERR, ex.Message)
                End Try
            End If

            If nodeForce.Checked = True Then
                Dim rowIndex As Integer = FindRowDgv(nodeForce.Text)
                Try
                    StyleStatus(rowIndex, "Performing", Color.Yellow)
                    Force.ListForces()
                    StyleStatus(rowIndex, "Finished", Color.Green)
                Catch ex As Exception
                    StyleStatus(rowIndex, "Error Occured", Color.Red)
                    Tools.OutputLog.Print(Tools.OutputLog.Type.ERR, ex.Message)

                End Try
            End If

            If nodeForceOut.Checked = True Then
                Dim rowIndex As Integer = FindRowDgv(nodeForceOut.Text)
                Try
                    StyleStatus(rowIndex, "Performing", Color.Yellow)
                    ForceOut.MeasureForceMarker()
                    ForceOut.ListForceOutputs()
                    StyleStatus(rowIndex, "Finished", Color.Green)
                Catch ex As Exception
                    StyleStatus(rowIndex, "Error Occured", Color.Red)
                    Tools.OutputLog.Print(Tools.OutputLog.Type.ERR, ex.Message)
                End Try
            End If

            If nodeIfctn.Checked = True Then
                Dim rowIndex As Integer = FindRowDgv(nodeIfctn.Text)
                Try
                    StyleStatus(rowIndex, "Performing", Color.Yellow)
                    Ifctn.ListIfctn()
                    StyleStatus(rowIndex, "Finished", Color.Green)
                Catch ex As Exception
                    StyleStatus(rowIndex, "Error Occured", Color.Red)
                    Tools.OutputLog.Print(Tools.OutputLog.Type.ERR, ex.Message)
                End Try
            End If

            If nodeComm.Checked = True Then
                Dim rowIndex As Integer = FindRowDgv(nodeComm.Text)
                Try
                    StyleStatus(rowIndex, "Performing", Color.Yellow)
                    Receiver.ListReceiver()
                    Sender.ListSender()
                    StyleStatus(rowIndex, "Finished", Color.Green)
                Catch ex As Exception
                    StyleStatus(rowIndex, "Error Occured", Color.Red)
                    Tools.OutputLog.Print(Tools.OutputLog.Type.ERR, ex.Message)
                End Try
            End If

            If nodeExpression.Checked = True Then
                Dim rowIndex As Integer = FindRowDgv(nodeExpression.Text)
                Try
                    StyleStatus(rowIndex, "Performing", Color.Yellow)
                    Expressions.ListExpressionCutForce()
                    StyleStatus(rowIndex, "Finished", Color.Green)
                Catch ex As Exception
                    StyleStatus(rowIndex, "Error Occured", Color.Red)
                    Tools.OutputLog.Print(Tools.OutputLog.Type.ERR, ex.Message)
                End Try
            End If

            If nodeSolve.Checked = True Then
                Dim rowIndex As Integer = FindRowDgv(nodeSolve.Text)
                Try
                    StyleStatus(rowIndex, "Performing", Color.Yellow)
                    Equilibrium.ListEquilibrium()
                    Preload.ListPreload()
                    Eigenvalue.ListEigenvalues()
                    StyleStatus(rowIndex, "Finished", Color.Green)
                Catch ex As Exception
                    StyleStatus(rowIndex, "Error Occured", Color.Red)
                    Tools.OutputLog.Print(Tools.OutputLog.Type.ERR, ex.Message)

                End Try
            End If
            If nodeSubstr.Checked = True Then
                Dim rowIndex As Integer = FindRowDgv(nodeSubstr.Text)
                Try
                    StyleStatus(rowIndex, "Performing", Color.Yellow)
                    Substructure.ListSubstrInfo()
                    Substructure.ListSubstrMassProperty()
                    StyleStatus(rowIndex, "Finished", Color.Green)
                Catch ex As Exception
                    StyleStatus(rowIndex, "Error Occured", Color.Red)
                    Tools.OutputLog.Print(Tools.OutputLog.Type.ERR, ex.Message)
                End Try
            End If
            Dim DGVs As New List(Of DataGridView)
            GetDataGridViews(GUI.tabCheckLocal, DGVs)
            For Each dgv As DataGridView In DGVs
                For Each col As DataGridViewColumn In dgv.Columns
                    If RemoveWhitespace(col.HeaderText) = String.Empty Then
                        col.Visible = False
                    End If
                Next
            Next
        End Sub

Open in new window


000497.jpg
HakanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

anarki_jimbelSenior DeveloperCommented:
First, the code in the error handlers is same. It would be better to move it to a separate function like:

Private Sub HandleError(rowIndex as Integer, ex As Exception)
                    StyleStatus(rowIndex, "Error Occured", Color.Red)
                    Tools.OutputLog.Print(Tools.OutputLog.Type.ERR, ex.Message)
End Sub

Open in new window


Benefit: if you need to make a change in error handling or , e.g., in a message - you will need to change in one place.

Second. There is a good common rule - to have a method to fit one screen. So you don't need to scroll a lot.

Therefore, I'd move node handling code for each node to a separate method. And would try to provide good self-explanatory names for these methods.

Also, I'd declare all string values like ""Performing", "Finished" as string constants, and pass these constants to methods.

OK, eventually you may have a method like below:

Private Sub ProcessNode(node as TreeNode)
            If node.Checked Then
                Dim rowIndex As Integer = FindRowDgv(node.Text)
                Try
                    StyleStatus(rowIndex, "Performing", Color.Yellow)
                    ' Here should be a method that finds out what operations has to be run, based on a node text, e.g.
                    RunOperations(node.text)
                    StyleStatus(rowIndex, "Finished", Color.Green)
                Catch ex As Exception
                    StyleStatus(rowIndex, "Error Occured", Color.Red)
                    Tools.OutputLog.Print(Tools.OutputLog.Type.ERR, ex.Message)
                End Try
            End If
End Sub

Open in new window


and run this method for each node in a loop.
I'd probably go this way.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.