C
Charles Law
I have a base class and derived classes that relate to a set of documents I
process. e.g. DocBase, DocA, DocB, DocC. The processing of each document is
handled in teh derived classes, as you might imagine, but the base class has
properties and methods that are common to all documents.
The processing of each docuemnt can result in a range of document specific
errors, which I want to raise as events on the document object, but there
are some events that are common to all documents, so I would like to have
those raised in the base class. In fact, I want to raise all events through
a delegate in the base class so that the object owners don't have to do too
much work.
So, I have in mind something like this for the caller:
Main
Sub ProcessDocA
Dim doc As DocBase = New DocA(path)
doc.Process(AddressOf DocA_ProcessError) <--- Problem here
End Sub
Sub ProcessDocB
Dim doc As DocBase = New DocB(path)
doc.Process(AddressOf DocB_ProcessError) <--- Problem here
End Sub
Sub DocA_ProcessError(ByVal sender As Object, ByVal e As
DocAProcessErrorEventArgs)
' Prompt User
End Sub
Sub DocB_ProcessError(ByVal sender As Object, ByVal e As
DocBProcessErrorEventArgs)
' Prompt User
End Sub
My base class looks like this
Public MustInherit Class DocBase
Delegate Sub ProcessErrorEventHandler(ByVal sender As Object, ByVal
e As EventArgs)
Protected ProcessErrorCallBack As ProcessErrorEventHandler
MustOverride Sub Process(ByVal callback As ProcessErrorEventHandler)
Protected Sub OnProcessError(ByVal e As EventArgs)
ProcessErrorCallBack.Invoke(Me, e)
End Sub
End Class
My derived class looks like this
Public Class DocA
Inherits DocBase
Public Overrides Sub Process(ByVal processErrorCallback As
ProcessErrorEventHandler)
MyBase.ProcessErrorCallback = processErrorCallback
DoProcessing()
End Sub
Private Sub Do Processing()
' Trap Error
MyBase.OnProcessError(New DocAProcessErrorEventArgs("Error in
Doc A"))
End Sub
End Class
The problem is where I have arrowed above, becase the compiler doesn't like
an implicit narrowing conversion from DocA_ProcessError() to
ProcessErrorEventHandler.
I know this all looks very complicated, and perhaps unnecessarily so, but
can anyone suggest a better, more generic way to do this.
TIA
Charles
process. e.g. DocBase, DocA, DocB, DocC. The processing of each document is
handled in teh derived classes, as you might imagine, but the base class has
properties and methods that are common to all documents.
The processing of each docuemnt can result in a range of document specific
errors, which I want to raise as events on the document object, but there
are some events that are common to all documents, so I would like to have
those raised in the base class. In fact, I want to raise all events through
a delegate in the base class so that the object owners don't have to do too
much work.
So, I have in mind something like this for the caller:
Main
Sub ProcessDocA
Dim doc As DocBase = New DocA(path)
doc.Process(AddressOf DocA_ProcessError) <--- Problem here
End Sub
Sub ProcessDocB
Dim doc As DocBase = New DocB(path)
doc.Process(AddressOf DocB_ProcessError) <--- Problem here
End Sub
Sub DocA_ProcessError(ByVal sender As Object, ByVal e As
DocAProcessErrorEventArgs)
' Prompt User
End Sub
Sub DocB_ProcessError(ByVal sender As Object, ByVal e As
DocBProcessErrorEventArgs)
' Prompt User
End Sub
My base class looks like this
Public MustInherit Class DocBase
Delegate Sub ProcessErrorEventHandler(ByVal sender As Object, ByVal
e As EventArgs)
Protected ProcessErrorCallBack As ProcessErrorEventHandler
MustOverride Sub Process(ByVal callback As ProcessErrorEventHandler)
Protected Sub OnProcessError(ByVal e As EventArgs)
ProcessErrorCallBack.Invoke(Me, e)
End Sub
End Class
My derived class looks like this
Public Class DocA
Inherits DocBase
Public Overrides Sub Process(ByVal processErrorCallback As
ProcessErrorEventHandler)
MyBase.ProcessErrorCallback = processErrorCallback
DoProcessing()
End Sub
Private Sub Do Processing()
' Trap Error
MyBase.OnProcessError(New DocAProcessErrorEventArgs("Error in
Doc A"))
End Sub
End Class
The problem is where I have arrowed above, becase the compiler doesn't like
an implicit narrowing conversion from DocA_ProcessError() to
ProcessErrorEventHandler.
I know this all looks very complicated, and perhaps unnecessarily so, but
can anyone suggest a better, more generic way to do this.
TIA
Charles