473,320 Members | 1,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Can't figure out this issue with Generics

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<TeventArgs)
{
target(this, eventArgs);
}

public static EventHandler<EventArgs<T>>
Create<T>(EventHandler<EventArgs<T>target)
{
EventShimWithArgs<Tshim = 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.EventAr gs<T>
[c:\PMDRepository\Tools\OfficeStudio\Source\Infrast ructure\Infrastructure.Interface\bin\Debug\Infrast ructure.Interface.dll]'

to

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

C:\PMDRepository\Tools\OfficeStudio\Server\OfficeS tudioServerInterfaces\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
Jan 7 '08 #1
2 1954
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.c s 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.c s 67 8
ConsoleApplication2

So:
Rar<Tand Create<Tshould perhaps not be generic methods, but just
members of the generic class?
i.e. Rar(object sender, EventArgs<TeventArgs) 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
Jan 7 '08 #2
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

"Marc Gravell" <ma**********@gmail.comwrote in message
news:31**********************************@y5g2000h sf.googlegroups.com...
>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.c s 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.c s 67 8
ConsoleApplication2

So:
Rar<Tand Create<Tshould perhaps not be generic methods, but just
members of the generic class?
i.e. Rar(object sender, EventArgs<TeventArgs) 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

Jan 7 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Fred | last post by:
Is there a way to use generics with vs.net 2003?? I was searching around and I saw gyro but does it works with vs.net 2003?? if it does how am I suppouse to install it. If it doesnt is there...
27
by: Bernardo Heynemann | last post by:
How can I use Generics? How can I use C# 2.0? I already have VS.NET 2003 Enterprise Edition and still can´t use generics... I´m trying to make a generic collection myCollection<vartype> and...
13
by: Anders Borum | last post by:
Hello! Now that generics are introduces with the next version of C#, I was wondering what kind of performance gains we're going to see, when switching from e.g. the general hashtable to a...
4
by: Peter Olcott | last post by:
I want to be able to make my .NET applications run just as fast as unmanaged C++. From my currently somewhat limited understanding of the .NET framework and the C# language, it seems that...
8
by: Chris Dunaway | last post by:
The next version of VS.Net (Whidbey) promises to add generics support to VB. I have a vague remembrance of Templates in C++, but I guess I need a refresher. What will generics allow us to do? ...
12
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as...
9
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal...
2
by: Mike Davies | last post by:
Hi, I want to do this but can;t figure it out! :-( Can I even do it? public static T Get4Bytes<T>( byte ptr , ref int Index) { uint ui = 0; try {
2
by: Andrew Backer | last post by:
I hope someone out there can help me with this, because I am stuck. My problem is that I have an instance of a generic class, but I don't know ahead of time what the type parameter used was. In...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.