Creating/Raising An Event In Class and Other Class Subscribe To It?

I have a database extension class that I use. I was the database to raise an event when commands are executed and I want to pass the error number and message into that event. Then, classes which use the database extension class could subscribe or listen for those data base errors and log them or display them or do whatever with them. I am repeating a lot of code to read the database error and then handle how to display it in each context.

I can create an Event Definition in the database class, but I cannot seem to find a way to attach to it within another class. I think i’m using it incorrectly.

Any ideas or alternatives on how to do this?

Any subclass can “listen” to an event by implementing that event. Any other class that uses your database subclass can implement that even by using AddHandler/RemoveHandler.

We handled this by creating a RuntimeException subclass with a Shared method. Our code looks something like this:

rs = db.SQLSelect( ... )
DbException.RaiseIfError db, "Some meaningful message"

Shared Sub DbException.RaiseIfError (db as Database, msg as String)
  if db.Error then
    dim err as new DbException
    err.Message = db.ErrorMessage + if( msg <> "", ": " + msg, "" )
    raise err
End Sub

If you’re looking for an application-wide publish/subscribe event bus kind of thing, there are a few around. I like this one:

https://github.com/ianmjones/EventBusIJ

[quote=392187:@Kem Tekinay]Any subclass can “listen” to an event by implementing that event. Any other class that uses your database subclass can implement that even by using AddHandler/RemoveHandler.

We handled this by creating a RuntimeException subclass with a Shared method. Our code looks something like this:
[/quote]

@Kem Tekinay
I’m not subclassing. The database class which generates the event is a property(dependency injection) In the class where I want to be able to subscribe

When Using AddHandler I get the error “Only Objects Can be Used With AddHandler”

Hi,

I don’t know if this is exactly what your are looking for, but maybe you can implement the Observer Design Pattern into your Database extension class. This way, other objects interested in receiving notifications just need to “suscribe” to it.

You can read about this in this post from the Xojo Blog.

Hope it helps!

Javier

Can you show us how you’re using AddHandler?

From error and description, I have the feeling you are trying to address the database class’ events which does not work. Addhandler works only on instances of classes which implement events.

Let’s assume your class is called yourClass and it has a property db As DatabaseWrapper.
DatabaseWrapper wraps a database of some kind – I use SQLite in this example –, and because you have to check for database errors constantly, you created a method that checks for db.error and if true fires an error event which you created and where you forward the error code and message to.
In code, without all View Behavior properties, it looks something like that:

[code]Protected Class DatabaseWrapper
Private Sub CheckError()
if db.Error then RaiseEvent Error (db.ErrorCode, db.ErrorMessage)
End Sub

            Event Error(ErrorCode as Integer, ErrorMessage as String)
	  
	Private db As sqlitedatabase

End Class[/code]

If yourClass now has a property of this class called myWrapper, you could tweak the error event of this property with an

AddHandler myWrapper.Error, (Weak)AddressOf ErrorHandler.

ErrorHandler now could be a method of the myClass class with the parameters

Protected Sub ErrorHandler (Wrapper As DatabaseWrapper, ErrorCode as Integer, ErrorMessage as String) // Alert the user somehow End Sub

An AddHandler will always forward the event firing instance as first parameter before the other event parameters, so you must insert it like in the definition above.
Don’t forget to use RemoveHandler if you are reassigning the property and on cleanup.