none
Calling same code from different screens RRS feed

  • Question

  •  I have searched but I have not been able to find what I need.  Probably not using the correct terms.  If this has been answered please point me to the answer.

    I have a program with several different screens.  In three of those screens I perform the same rather lengthy calculations.  How can I write this calculation once and have it called from those three screens?

    Thanks in advance.


    Philip


    • Edited by Philip Potts Thursday, March 7, 2019 12:50 PM spelling correction
    Thursday, March 7, 2019 12:49 PM

Answers

  • Hello,

    Here is a pattern to consider.

    • The calculations should be performed in a class. The class would have a delegate that any form that has access to the newly created class can subscribe to the delegate and in turn response to what is passed. In the class, the event (which in the forms is a delegate) would pass information and those forms subscribed by delegate then get this information to do whatever with.
    • Create an instance of the class prior to showing the forms (see above).
    • Each form create should be passed the newly created instance of the class (see above).

    I have a project on OneDrive that shows how to create the above using one form but can be adapted to using multiple forms. Keep things to look at are in Account.vb, AccountDenialEventArgs and AccountgBalanceWarningEventArgs which are eposed via

    <NonSerialized()>
    Public Event AccountBalanceWarningEvent As AccountBalanceWarningEventHandler
    <NonSerialized()>
    Public Event AccountDenialEvent As AccountDenyingEventHandler

    Here is an example, two forms, one class.

    Class for calculations

    Public Class Operations
        Public Event SomeEvent As SomeEventHandler
    
        Public Function Add(
            pSomeItem As String,
            pValue1 As Integer,
            pValue2 As Integer) As Integer
    
            Dim result = pValue1 + pValue2
    
            RaiseEvent SomeEvent(Me,
                New AccountBalanceWarningEventArgs(pSomeItem, result))
    
            Return result
    
        End Function
    End Class
    Public Delegate Sub SomeEventHandler(
        sender As Object,
        e As AccountBalanceWarningEventArgs)
    
    Public Class AccountBalanceWarningEventArgs
        Inherits EventArgs
    
        Protected mValue As Integer
        Protected mName As String
    
        Public Sub New(pName As String, pValue As Integer)
            mValue = pValue
            mName = pName
        End Sub
        Public ReadOnly Property Name As String
            Get
                Return mName
            End Get
        End Property
        Public ReadOnly Property Value() As Integer
            Get
                Return mValue
            End Get
        End Property
    
    End Class

    Main form with two buttons

    Public Class Form1
        Private operation As New Operations
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim f As New Form2(operation)
            f.Show()
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            operation.Add("Karen", 1, 2)
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            AddHandler operation.SomeEvent, AddressOf Watch
        End Sub
        Private Sub Watch(sender As Object, e As AccountBalanceWarningEventArgs)
            MessageBox.Show($"{e.Name} - {e.Value}")
        End Sub
    End Class
    

    Form2, in this case form1 gets notifications.

    Public Class Form2
        Private operation As New Operations
        Public Sub New()
            InitializeComponent()
        End Sub
        Public Sub New(pOperations As Operations)
            InitializeComponent()
            operation = pOperations
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            operation.Add("Mary", 11, 22)
        End Sub
    End Class

    To keep each form watchers (delegates) to the form open

    Main form

    Public Class Form1
        Private operation As New Operations
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim f As New Form2()
            f.Show()
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            operation.Add("Karen", 1, 2)
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            AddHandler operation.SomeEvent, AddressOf Watch
        End Sub
        Private Sub Watch(sender As Object, e As AccountBalanceWarningEventArgs)
            MessageBox.Show($"{e.Name} - {e.Value}")
        End Sub
    End Class
    

    Form2

    Public Class Form2
        Private operation As New Operations
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            operation.Add("Mary in form2", 11, 22)
        End Sub
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            AddHandler operation.SomeEvent, AddressOf Watch
        End Sub
        Private Sub Watch(sender As Object, e As AccountBalanceWarningEventArgs)
            MessageBox.Show($"{e.Name} - {e.Value}")
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Thursday, March 7, 2019 1:12 PM
    Moderator

All replies

  • Hello,

    Here is a pattern to consider.

    • The calculations should be performed in a class. The class would have a delegate that any form that has access to the newly created class can subscribe to the delegate and in turn response to what is passed. In the class, the event (which in the forms is a delegate) would pass information and those forms subscribed by delegate then get this information to do whatever with.
    • Create an instance of the class prior to showing the forms (see above).
    • Each form create should be passed the newly created instance of the class (see above).

    I have a project on OneDrive that shows how to create the above using one form but can be adapted to using multiple forms. Keep things to look at are in Account.vb, AccountDenialEventArgs and AccountgBalanceWarningEventArgs which are eposed via

    <NonSerialized()>
    Public Event AccountBalanceWarningEvent As AccountBalanceWarningEventHandler
    <NonSerialized()>
    Public Event AccountDenialEvent As AccountDenyingEventHandler

    Here is an example, two forms, one class.

    Class for calculations

    Public Class Operations
        Public Event SomeEvent As SomeEventHandler
    
        Public Function Add(
            pSomeItem As String,
            pValue1 As Integer,
            pValue2 As Integer) As Integer
    
            Dim result = pValue1 + pValue2
    
            RaiseEvent SomeEvent(Me,
                New AccountBalanceWarningEventArgs(pSomeItem, result))
    
            Return result
    
        End Function
    End Class
    Public Delegate Sub SomeEventHandler(
        sender As Object,
        e As AccountBalanceWarningEventArgs)
    
    Public Class AccountBalanceWarningEventArgs
        Inherits EventArgs
    
        Protected mValue As Integer
        Protected mName As String
    
        Public Sub New(pName As String, pValue As Integer)
            mValue = pValue
            mName = pName
        End Sub
        Public ReadOnly Property Name As String
            Get
                Return mName
            End Get
        End Property
        Public ReadOnly Property Value() As Integer
            Get
                Return mValue
            End Get
        End Property
    
    End Class

    Main form with two buttons

    Public Class Form1
        Private operation As New Operations
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim f As New Form2(operation)
            f.Show()
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            operation.Add("Karen", 1, 2)
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            AddHandler operation.SomeEvent, AddressOf Watch
        End Sub
        Private Sub Watch(sender As Object, e As AccountBalanceWarningEventArgs)
            MessageBox.Show($"{e.Name} - {e.Value}")
        End Sub
    End Class
    

    Form2, in this case form1 gets notifications.

    Public Class Form2
        Private operation As New Operations
        Public Sub New()
            InitializeComponent()
        End Sub
        Public Sub New(pOperations As Operations)
            InitializeComponent()
            operation = pOperations
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            operation.Add("Mary", 11, 22)
        End Sub
    End Class

    To keep each form watchers (delegates) to the form open

    Main form

    Public Class Form1
        Private operation As New Operations
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim f As New Form2()
            f.Show()
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            operation.Add("Karen", 1, 2)
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            AddHandler operation.SomeEvent, AddressOf Watch
        End Sub
        Private Sub Watch(sender As Object, e As AccountBalanceWarningEventArgs)
            MessageBox.Show($"{e.Name} - {e.Value}")
        End Sub
    End Class
    

    Form2

    Public Class Form2
        Private operation As New Operations
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            operation.Add("Mary in form2", 11, 22)
        End Sub
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            AddHandler operation.SomeEvent, AddressOf Watch
        End Sub
        Private Sub Watch(sender As Object, e As AccountBalanceWarningEventArgs)
            MessageBox.Show($"{e.Name} - {e.Value}")
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Thursday, March 7, 2019 1:12 PM
    Moderator
  • Karen,

    You ROCK!  I can never thank you enough for all the help you have provided so I will just say Thank You again.


    Philip

    Friday, March 8, 2019 1:01 AM
  • Happy to help Philp :-)

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, March 8, 2019 1:29 AM
    Moderator