Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / October 2007

Tip: Looking for answers? Try searching our database.

Generic delegate question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Barry Gilbert - 23 Oct 2007 16:19 GMT
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.
Jon Skeet [C# MVP] - 23 Oct 2007 18:03 GMT
> I have a class that has a generic method that looks something like this:
>
[quoted text clipped - 10 lines]
> 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.

Signature

Jon Skeet - <skeet@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

Barry Gilbert - 23 Oct 2007 19:49 GMT
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

> > I have a class that has a generic method that looks something like this:
> >
[quoted text clipped - 19 lines]
>
> It's hard to say without knowing more information, I'm afraid.
Jon Skeet [C# MVP] - 23 Oct 2007 20:56 GMT
> 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?

Signature

Jon Skeet - <skeet@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

Barry Gilbert - 23 Oct 2007 21:27 GMT
Jon,

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

Thanks for your help.

> > Thanks for your reply.
> >
[quoted text clipped - 19 lines]
> That's worked fine for me, although I've only done it once. Would that
> help you?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.