Home All Groups Group Topic Archive Search About

Generic delegate question

Author
23 Oct 2007 3:19 PM
Barry Gilbert
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.

Author
23 Oct 2007 5:03 PM
Jon Skeet [C# MVP]
Barry Gilbert <BarryGilb***@discussions.microsoft.com> wrote:
Show quote
> 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 Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Author
23 Oct 2007 6:49 PM
Barry Gilbert
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

Show quote
"Jon Skeet [C# MVP]" wrote:

> Barry Gilbert <BarryGilb***@discussions.microsoft.com> wrote:
> > 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 Skeet - <sk***@pobox.com>
> http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too
>
Author
23 Oct 2007 7:56 PM
Jon Skeet [C# MVP]
Barry Gilbert <BarryGilb***@discussions.microsoft.com> wrote:
> 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 Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Author
23 Oct 2007 8:27 PM
Barry Gilbert
Jon,

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

Thanks for your help.

Show quote
"Jon Skeet [C# MVP]" wrote:

> Barry Gilbert <BarryGilb***@discussions.microsoft.com> wrote:
> > 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 Skeet - <sk***@pobox.com>
> http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too
>

AddThis Social Bookmark Button