Generics

R

ReneMarxis

Hello

I have some problem with events and generics. I'd like to raise an
event that inone generic class that has generic arguments:

//=====
public class PageStatusEventArgs<T> : EventArgs {
T _status;
public PageStatusEventArgs (T status) {
_status = status;
}

public T Status {
get { return (_status); }
}
}


public delegate void NewStatusEventHandler<T> (object sender,
PageStatusEventArgs<T> e);

public class PageStatus<T> {

public event NewStatusEventHandler< PageStatus<T>,
PageStatusEventArgs<T>> StatusChanged;

private T _status;

public T Status{
get { return _status; }
set {
//set the status
_status = value;
//and raise the event
StatusChanged(this, _status);
}
}
}
//=====

I get an error on
public event NewStatusEventHandler< PageStatus<T>,
PageStatusEventArgs<T>> StatusChanged;

I know why (because i have to define NewStatusEventHandler also as
<T>) but i don't know how or where to put that additional T :)

Can someone help please?

_thanks
 
N

Nicholas Paldino [.NET/C# MVP]

ReneMarxis,

You only have one type parameter in the NewStatusEventHandler delegate.
Because of that, the declaration of your event should be just the type of T
that you would use in the PageStatusEventArgs class, or, in your case, this:

public class PageStatus<T>
{
public event NewStatusEventHandler<T> StatusChanged;

// Other stuff.
...
}

Hope this helps.
 
R

ReneMarxis

But if i do it like that i don't get the new set status in the event
right?
I mean i don't need the PageStatusEventArgs at all but have to read
the status via the Status-property. Thats not what i wanted to do. I
want the event to pass the new set Status in the event.
 
R

ReneMarxis

Ok got it ... prety easy if you know how to do it :)

public delegate void NewStatusEventHandler<T>(object sender, T e);

public class PageStatus<T> {
public event NewStatusEventHandler<T> StatusChanged;

private T _status;

public T Status{
get { return _status; }
set {
//set the status
_status = value;
//and raise the event
StatusChanged(this, _status);
}
}
}

Thanks for your help
 
R

ReneMarxis

But just of curiousity. How do i have to instantiate
public event NewStatusEventHandler
in order to get it work?
 
R

ReneMarxis

I mean the first posted version:

public delegate void NewStatusEventHandler<T> (object sender,
PageStatusEventArgs<T> e);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top