• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 352
  • Last Modified:

Extend a sub from another class

I have this setup (stripped example):

class: manager.vb
private shared function save(byval object as myobject) as boolean
  'saving of object
end function

webpage codebehind: default.aspx.vb
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
   manager.save(myobject)
End Sub

So this works just fine, the user clicks a button and the object is saved through the save function of manager.vb class.

Now I would like to create a second class, e.g. log.vb with for example this function:
class: log.vb
private shared function LogEvent() as boolean
  'add event to the log
end function

I want to call this log function without having to write additional code in default.aspx.vb
So basicly something like this:

private shared function LogEvent() as boolean
   if manager.save is called then
  'add event to the log
   end if
end function

How can I accomplish this?

ps. I know I could call LogEvent from the save function in manager.vb, but I would like to keep it seperate, and in case of log.vb being removed, not breaking manager.vb

Thank you!
0
Onthrax
Asked:
Onthrax
  • 6
  • 3
1 Solution
 
CodeCruiserCommented:
There are a few ways to achieve this. You can pass the logger class reference to the manager save method. Then in save method, check to see if the logger has been supplied and if it is then use the LogEvent method. The other approach is to declare a global logger and keep it in some shared place like session or application object and call it from different parts of the application.
0
 
OnthraxAuthor Commented:
Hi,

Thank you for your answer.

However neither is a perfect solution. Your first solution would require a code adjustment in all places the manager is called from, if the logger class ever was removed.
In this case it would be easier just to call the logger from the manager class, so I would only have to remove 1 line if it ever is removed.

In your second solution I would still have to call the logger 'manually' instead of automatically being triggered.

I have also looked into events, but as far as my research goes it seems I cannot handle an event from one class in another class file...

Perhaps what I want is simply not possible.
0
 
Dennis AriesCEO @ Arkro ITCommented:
The only way to do what you desire requires adding code to the base class.
Based on your description that the logger-class might be removed in the future, you might consider adding an event to your manager-class and hook into that.

That way you can add additional functionality to your save-code without altering that class later on.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
OnthraxAuthor Commented:
Hi,

Could you give a short example code of what you are proposing. I have been experimenting with events but have been unsuccesful thus far.

Thank you!
0
 
OnthraxAuthor Commented:
Dennis, could you please provide an example, I'd like to continue this route if it is possible...

Thank you.
0
 
Dennis AriesCEO @ Arkro ITCommented:
Very simple sample:

managerclass
Imports System.ComponentModel

Public Class Manager

    Private Const SAVEDEVENTNAME As String = "Manager_SavedEvent"
#Region "EventList"
    Private _events As EventHandlerList = Nothing

    Protected ReadOnly Property Events() As EventHandlerList
        Get
            If _events Is Nothing Then
                _events = New EventHandlerList()
            End If
            Return _events
        End Get
    End Property
#End Region
#Region "Events"
    Public Custom Event Manager_Saved As EventHandler
        AddHandler(ByVal value As EventHandler)
            Me.Events.AddHandler(SAVEDEVENTNAME, value)
        End AddHandler

        RemoveHandler(ByVal value As EventHandler)
            Me.Events.RemoveHandler(SAVEDEVENTNAME, value)
        End RemoveHandler

        RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim handler = CType(Me.Events(SAVEDEVENTNAME), EventHandler)
            If (handler IsNot Nothing) Then
                handler.Invoke(sender, e)
            End If
        End RaiseEvent
    End Event
#End Region

    Public Sub Save()
        ' Add your code here

        RaiseEvent Manager_Saved(Me, New EventArgs())
    End Sub

End Class

Open in new window


And a form (for demonstrationpurpose):
Public Class Form1
    Dim WithEvents manager As Manager

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        manager = New Manager()
        manager.Save()
    End Sub

    Private Sub manager_Manager_Saved(ByVal sender As Object, ByVal e As System.EventArgs) Handles manager.Manager_Saved
        ' Add your logging-code here
        MessageBox.Show("Hello World!")
    End Sub
End Class

Open in new window

0
 
OnthraxAuthor Commented:
Thank you for the example, which works fine.

However I am using a shared class for the manager class and after some thought a shared event is not such a good idea as this would trigger the handler for all users on that page and thus multiple logging code firing. I could either rebuilt the manager class to make it not shared or take a different route.

Thank you for your time. I learned a great deal about events and will certainly apply them when possible.
0
 
OnthraxAuthor Commented:
Thank you for the example, which works fine.

However I am using a shared class for the manager class and after some thought a shared event is not such a good idea as this would trigger the handler for all users on that page and thus multiple logging code firing. I could either rebuilt the manager class to make it not shared or take a different route.

Thank you for your time. I learned a great deal about events and will certainly apply them when possible.
0
 
Dennis AriesCEO @ Arkro ITCommented:
Glad to be of help

Dennis
0
 
OnthraxAuthor Commented:
Although I can't use this as I am using a shared class, this does answer the original question and will be usable in other situations, so therefore accepted as answer. Thanks again.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now