Link to home
Start Free TrialLog in
Avatar of Onthrax
OnthraxFlag for Netherlands

asked on

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!
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
Avatar of Onthrax

ASKER

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.
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.
Avatar of Onthrax

ASKER

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!
Avatar of Onthrax

ASKER

Dennis, could you please provide an example, I'd like to continue this route if it is possible...

Thank you.
ASKER CERTIFIED SOLUTION
Avatar of Dennis Aries
Dennis Aries
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Onthrax

ASKER

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.
Avatar of Onthrax

ASKER

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.
Glad to be of help

Dennis
Avatar of Onthrax

ASKER

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.