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 / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

Can't figure out this issue with Generics

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Steve K. - 07 Jan 2008 01:02 GMT
I am in over my head I think, I've got my brain running in circles trying to
figure this out.

I'm using remoting with a client and a server.  I need the server to raise
events on the client and after doing quite a bit of reading I found what I
think is the correct way.  The article I read encourages shims to pass the
delegate to server and back so that the client process doesn't need to be
passed.  I barely understand it, I will understand more when I get it
working.

Anyway, these "Shim" classes in the example were tied to a specific delegate
and I wanted to create something a little more generic and reusable.

Here is the code I'm having trouble with:
<code>
//  From the client
EventShimWithArgs<int[]>.Create(new
EventHandler<EventArgs<int[]>>(OnSomething));

//  EventArgs <T>
public class EventArgs<T> : EventArgs
{
   private T _data;

   public EventArgs(T data)
   {
       _data = data;
   }

   public T Data
   {
       get { return _data; }
   }
}

//  On the server
public class EventShimWithArgs<T> : MarshalByRefObject
{
   private EventHandler<EventArgs<T>> target;

   private EventShimWithArgs(EventHandler<EventArgs<T>> target)
   {
       this.target += target;
   }

   public void Rar<T>(object sender, EventArgs<T> eventArgs)
   {
       target(this, eventArgs);
   }

   public static EventHandler<EventArgs<T>>
Create<T>(EventHandler<EventArgs<T>> target)
   {
       EventShimWithArgs<T> shim = new EventShimWithArgs<T>(target);
       return new EventHandler<EventArgs<T>>( shim.Rar<T> );
   }
}
</code>

The line that is giving me my last compiler error is:
target(this, eventArgs);

The error is:
<error>
cannot convert from

'PMD.OfficeStudio.Infrastructure.Interface.EventArgs<T>
[c:\PMDRepository\Tools\OfficeStudio\Source\Infrastructure\Infrastructure.Interface\bin\Debug\Infrastructure.Interface.dll]'

to

'PMD.OfficeStudio.Infrastructure.Interface.EventArgs<T>
[c:\PMDRepository\Tools\OfficeStudio\Source\Infrastructure\Infrastructure.Interface\bin\Debug\Infrastructure.Interface.dll]'

C:\PMDRepository\Tools\OfficeStudio\Server\OfficeStudioServerInterfaces\EventShimWithArgs.cs
OfficeStudioServer.Interface 23 26
</error>

Now... it's complaining about not being able to convert by the types it says
it can't covert are the SAME!

I'm stumped, anyone see the problem?

Thanks for any help!
Steve
Marc Gravell - 07 Jan 2008 05:18 GMT
> the types it says it can't covert are the SAME!
> I'm stumped, anyone see the problem?

Different T... errors are one thing, but also look at the *warnings*:
Warning    1    Type parameter 'T' has the same name as the type parameter
from outer type 'EventShimWithArgs<T>'    d:\dump\wf
\ConsoleApplication2\ConsoleApplication2\Program.cs    60    21
ConsoleApplication2
Warning    2    Type parameter 'T' has the same name as the type parameter
from outer type 'EventShimWithArgs<T>'    d:\dump\wf
\ConsoleApplication2\ConsoleApplication2\Program.cs    67    8
ConsoleApplication2

So:
Rar<T> and Create<T> should perhaps not be generic methods, but just
members of the generic class?
i.e. Rar(object sender, EventArgs<T> eventArgs) etc

If you *need* them as generic methods inside the generic class, then
pick a different name for the arg, i.e.
i.e. Rar<TSomethingElse>(...)
(pick a meaningful name...)

Marc
sklett - 07 Jan 2008 17:06 GMT
Hi Marc,

Great help, thanks so much.  The warnings held the key!  I've also modifed
the design per your suggestions and it seems to be working nicely now and I
learned something, always a good thing.

Take care,
Steve

>> the types it says it can't covert are the SAME!
>> I'm stumped, anyone see the problem?
[quoted text clipped - 20 lines]
>
> Marc

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.