Move code into class

Scot King 116 Reputation points
2021-10-05T03:09:32.577+00:00

I have the following below code which I'm using to initialize a modalpopupextender . Due to the fact that there are 106 buttons going down the page, and 96 lines of code for each button as shown below, this adds 9964 lines of code to the already existing 2800 lines of code. My question : is it possible to send what is below to a class? I don't want to have the page so large that it becomes slow when loading. Can a class access the page elements (textbox.txt) by somehow passing the page event args (sender As Object, e As EventArgs) to the class? Is it as simple as passing sender and e to the class to somethings like this?

Public Class Feepopup
    Public Shared Function discfee(ByVal sender As Object, e As EventArgs)

    End Function
End Class

Protected Sub btnloanleveladj1_Click(sender As Object, e As EventArgs)
     txtfeename.Text = ddloanleveladj1.SelectedValue
            Dim borfee As Decimal, selfee As Decimal, bkrfee As Decimal, lenfee As Decimal, ptyfee As Decimal, corfee As Decimal, totpercent As Decimal = 0
            txtborfee.Text = ""
            txtsellerfee.Text = ""
            txtbrokerfee.Text = ""
            txtlenderfee.Text = ""
            txt3partyfee.Text = ""
            txtcorresfee.Text = ""
            Dim tblfeesplitadapter As New FeesplitTableAdapter
            Dim feesplit As New DataSet1.FeesplitDataTable
            feesplit = tblfeesplitadapter.GetData(CInt(Session("ApplicantID")))
            For Each feesplitrow As DataSet1.FeesplitRow In feesplit
                ddparty.SelectedIndex = feesplitrow.llp1pty
                Rbsplit.SelectedIndex = feesplitrow.llp1split
                If feesplitrow.llp1split = 0 Then
                    Dim results As String() = feesplitrow.llp1pntamt.Split(",")
                    txtborpercent.Text = results(0)
                    txtsellerpercent.Text = results(1)
                    txtbrokerpercent.Text = results(2)
                    txtlenderpercent.Text = results(3)
                    txt3partypercent.Text = results(4)
                    txtcorrespercent.Text = results(5)
                    If results(0) <> "" And txtloanleveladj1total.Text <> "" Then
                        borfee = txtloanleveladj1total.Text * results(0) / 100
                        txtborfee.Text = borfee
                        totpercent = results(0)
                    End If
                    If results(1) <> "" And txtloanleveladj1total.Text <> "" Then
                        selfee = txtloanleveladj1total.Text * results(1) / 100
                        txtsellerfee.Text = selfee
                        totpercent = totpercent + results(1)
                    End If
                    If results(2) <> "" And txtloanleveladj1total.Text <> "" Then
                        bkrfee = txtloanleveladj1total.Text * results(2) / 100
                        txtbrokerfee.Text = bkrfee
                        totpercent = totpercent + results(2)
                    End If
                    If results(3) <> "" And txtloanleveladj1total.Text <> "" Then
                        lenfee = txtloanleveladj1total.Text * results(3) / 100
                        txtlenderfee.Text = lenfee
                        totpercent = totpercent + results(3)
                    End If
                    If results(4) <> "" And txtloanleveladj1total.Text <> "" Then
                        ptyfee = txtloanleveladj1total.Text * results(4) / 100
                        txt3partyfee.Text = ptyfee
                        totpercent = totpercent + results(4)
                    End If
                    If results(5) <> "" And txtloanleveladj1total.Text <> "" Then
                        corfee = txtloanleveladj1total.Text * results(5) / 100
                        txtcorresfee.Text = corfee
                        totpercent = totpercent + results(5)
                    End If
                    txtfeepercent.Text = totpercent
                    txtfeeamount.Text = borfee + selfee + bkrfee + lenfee + ptyfee + corfee
                Else
                    Dim results As String() = feesplitrow.llp1pntamt.Split(",")
                    txtborfee.Text = results(0)
                    txtsellerfee.Text = results(1)
                    txtbrokerfee.Text = results(2)
                    txtlenderfee.Text = results(3)
                    txt3partyfee.Text = results(4)
                    txtcorresfee.Text = results(5)
                    If results(0) <> "" Then
                        borfee = results(0)
                    End If
                    If results(1) <> "" Then
                        selfee = results(1)
                    End If
                    If results(2) <> "" Then
                        bkrfee = results(2)
                    End If
                    If results(3) <> "" Then
                        lenfee = results(3)
                    End If
                    If results(4) <> "" Then
                        ptyfee = results(4)
                    End If
                    If results(5) <> "" Then
                        corfee = results(5)
                    End If
                    txtfeeamount.Text = borfee + selfee + bkrfee + lenfee + ptyfee + corfee
                End If
            Next
            If feesplit.Rows.Count = 0 Then
                InsertFeesplit()
                txtborpercent.Text = "100"
                txtborfee.Text = txtloanleveladj1total.Text
                txtfeepercent.Text = "100"
                txtfeeamount.Text = txtloanleveladj1total.Text
            End If
            HiddenField1.Value = txtloanleveladj1total.Text
            HiddenField2.Value = "llp1"
            Me.ModalPopupExtender1.Show()
        End Sub
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,564 questions
{count} votes

Accepted answer
  1. Scot King 116 Reputation points
    2021-10-06T00:37:21.027+00:00

    Hi Karen, thanks for your suggestions. I have not heard of the Personally ReSharper before and I will check this out. It appears to be a paid for program. I have already created a class to handle sql functions and it is working. After some experimentation, I correctly suspected that I could IN FACT pass sender and e to another sub...How about that? You didn't mention that in your post. I have the result as shown below:

    Protected Sub initialize(sender As Object, e As EventArgs)
    txtborfee.Text = ""
    txtsellerfee.Text = ""
    txtbrokerfee.Text = ""
    txtlenderfee.Text = ""
    txt3partyfee.Text = ""
    txtcorresfee.Text = ""
    End Sub
    Protected Sub assignpercent(sender As Object, e As EventArgs, ByVal results As Array, ByVal amount As Decimal)
    Dim borfee As Decimal, selfee As Decimal, bkrfee As Decimal, lenfee As Decimal, ptyfee As Decimal, corfee As Decimal, totpercent As Decimal = 0
    txtborpercent.Text = results(0)
    txtsellerpercent.Text = results(1)
    txtbrokerpercent.Text = results(2)
    txtlenderpercent.Text = results(3)
    txt3partypercent.Text = results(4)
    txtcorrespercent.Text = results(5)
    If results(0) <> "" Then
    borfee = amount * results(0) / 100
    txtborfee.Text = borfee
    totpercent = results(0)
    End If
    If results(1) <> "" Then
    selfee = amount * results(1) / 100
    txtsellerfee.Text = selfee
    totpercent = totpercent + results(1)
    End If
    If results(2) <> "" Then
    bkrfee = amount * results(2) / 100
    txtbrokerfee.Text = bkrfee
    totpercent = totpercent + results(2)
    End If
    If results(3) <> "" Then
    lenfee = amount * results(3) / 100
    txtlenderfee.Text = lenfee
    totpercent = totpercent + results(3)
    End If
    If results(4) <> "" Then
    ptyfee = amount * results(4) / 100
    txt3partyfee.Text = ptyfee
    totpercent = totpercent + results(4)
    End If
    If results(5) <> "" Then
    corfee = amount * results(5) / 100
    txtcorresfee.Text = corfee
    totpercent = totpercent + results(5)
    End If
    txtfeepercent.Text = totpercent
    txtfeeamount.Text = borfee + selfee + bkrfee + lenfee + ptyfee + corfee
    End Sub
    Protected Sub assignamt(sender As Object, e As EventArgs, ByVal results As Array)
    Dim borfee As Decimal, selfee As Decimal, bkrfee As Decimal, lenfee As Decimal, ptyfee As Decimal, corfee As Decimal, totpercent As Decimal = 0
    txtborfee.Text = results(0)
    txtsellerfee.Text = results(1)
    txtbrokerfee.Text = results(2)
    txtlenderfee.Text = results(3)
    txt3partyfee.Text = results(4)
    txtcorresfee.Text = results(5)
    If results(0) <> "" Then
    borfee = results(0)
    End If
    If results(1) <> "" Then
    selfee = results(1)
    End If
    If results(2) <> "" Then
    bkrfee = results(2)
    End If
    If results(3) <> "" Then
    lenfee = results(3)
    End If
    If results(4) <> "" Then
    ptyfee = results(4)
    End If
    If results(5) <> "" Then
    corfee = results(5)
    End If
    txtfeeamount.Text = borfee + selfee + bkrfee + lenfee + ptyfee + corfee
    End Sub
    Protected Sub btndiscountfee_Click(sender As Object, e As EventArgs)
    initialize(sender, e)
    txtfeename.Text = "Loan Discount"
    Dim tblfeesplitadapter As New FeesplitTableAdapter
    Dim feesplit As New DataSet1.FeesplitDataTable
    feesplit = tblfeesplitadapter.GetData(CInt(Session("ApplicantID")))
    For Each feesplitrow As DataSet1.FeesplitRow In feesplit
    ddparty.SelectedIndex = feesplitrow.discpty
    Rbsplit.SelectedIndex = feesplitrow.discsplit
    If feesplitrow.discsplit = 0 Then
    Dim results As String() = feesplitrow.discpntamt.Split(",")
    If txtdiscounttotal.Text <> "" Then
    assignpercent(sender, e, results, txtdiscounttotal.Text)
    End If
    Else
    Dim results As String() = feesplitrow.discpntamt.Split(",")
    assignamt(sender, e, results)
    End If
    Next
    If feesplit.Rows.Count = 0 Then
    InsertFeesplit()
    txtborpercent.Text = "100"
    txtborfee.Text = txtdiscounttotal.Text
    txtfeepercent.Text = "100"
    txtfeeamount.Text = txtdiscounttotal.Text
    End If
    HiddenField1.Value = txtdiscounttotal.Text
    HiddenField2.Value = "disc"
    Me.ModalPopupExtender1.Show()
    End Sub


1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,031 Reputation points
    2021-10-05T10:01:20.42+00:00

    Here are some things to consider for reorganizing current code.

    No matter what direction is taken, if you have source control, create a new branch before starting this process of reorganizing code. Learn about what options Visual Studio has to assist with refactoring code. Personally ReSharper makes reorganizing code child’s play.

    Next step, turn Option Strict On, Option Infer On. Currently it looks like you have these not setup with On.

    Next steps are going to be time consuming coupled with trial and error as the best way to handle revamping form code to classes involves the following.

    Devoid the form of any components such as DataSet and similar components in favor of a data class which delegates/events are used to communicate with form code.

    Use data binding via BindingSource (and possibly BindingList) components.

    Setup container classes implementing INotifyPropertyChanged Interface in tangent with BindingSource/BindingList for change notification when properties in container classes change values.

    Learn (if you have not done so before) how to write unit test and test often rather than relying on running the app to test functionality.

    Bottom line, many new and intermediate level coders fall into a trap which is writing all (or most) of their code in a form while a season developer learns to code with a data layer, business layer and user interface layer. So you are currently in this trap, is there a better way out? Perhaps, an alternate idea is to start from scratch in a new solution that only shares business logic for the project and uses the recommendations above. More likely than not this will not be an option for you but it needs to be put out there.

    0 comments No comments