Generic delegate question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a class that has a generic method that looks something like this:

Public Sub Initialize(Of T As MyBusinessListBase(Of T, C), _
C As MyBusinessBase(Of C)) _
(ByRef dataSource As MyBusinessListBase(Of T, C), _
ByVal reportName As String, ByVal keyProperty As String, _
ByVal callBack As ReportChildCollectionCallback(Of T, C))
' Do stuff to initialize a reportViewer report...
End Sub

Later, this object raises an event. In that event handler, I want to invoke
the callback routine passed into the Initialize method. I'm having trouble
understanding how to store a reference to the callback routine at the class
level so that I can use it in the event handler.
 
Barry Gilbert said:
I have a class that has a generic method that looks something like this:

Public Sub Initialize(Of T As MyBusinessListBase(Of T, C), _
C As MyBusinessBase(Of C)) _
(ByRef dataSource As MyBusinessListBase(Of T, C), _
ByVal reportName As String, ByVal keyProperty As String, _
ByVal callBack As ReportChildCollectionCallback(Of T, C))
' Do stuff to initialize a reportViewer report...
End Sub

Later, this object raises an event. In that event handler, I want to invoke
the callback routine passed into the Initialize method. I'm having trouble
understanding how to store a reference to the callback routine at the class
level so that I can use it in the event handler.

I don't "do" VB generic declarations, so I can't tell for sure if this
is actually a generic method or just a method within a generic class.
If it's a generic method, you'll have difficulties - you could store
just a Delegate reference and use DynamicInvoke, but it would be better
to make it strongly typed somehow - you may need to introduce an extra
generic type, or parameterize the existing type further.

It's hard to say without knowing more information, I'm afraid.
 
Jon,

Thanks for your reply.

This is a generic method inside a User Control class. I considered making
the class generic, but I found that it's difficult (impossible?) to declare a
user control or form as generic. Am I right about this? I think this would
solve my problem.

Barry
 
Barry Gilbert said:
Thanks for your reply.

This is a generic method inside a User Control class. I considered making
the class generic, but I found that it's difficult (impossible?) to declare a
user control or form as generic. Am I right about this? I think this would
solve my problem.

I've made a user control generic in the past, but then I've had to
create a specific (non-generic) type derived from it in order to use
it, e.g.

public class WhizzyControl<T> : UserControl
{
.... Lots of implementation
}

public class Int32WhizzyControl : WhizzyControl<int>
{
// No code whatsoever
}

That's worked fine for me, although I've only done it once. Would that
help you?
 
Jon,

I've been avoiding taking this on, but I suppose it's time to give it a go.

Thanks for your help.
 
Back
Top