E
Elder Hyde
Hey all,
A class of mine needs to tell the outside world when its buffer is not
empty. The problem is that C# seems to force you to put the
event-raising code in the base class. To illustrate, consider what I'll
do in Java:
public interface DataAvailabilityListener extends java.util.EventListener {
void dataArrived(DataAvailabilityEvent event);
}
then, in my base class, I can do this:
public abstract class SmartQueue {
addDataAvailabilityListener(DataAvailabilityListener listener);
}
then in my implementation class, say a memory-backed smart queue, I can
do this:
public class MemorySmartQueue implements SmartQueue {
public void poolForData() {
// data available!
while(iter.hasNext()) {
DataAvailabilityListener lis =
(DataAvailabilityListener)iter.next();
lis.dataArrived(someEvent);
}
}
}
Simple and straighforward. However, consider a C# implementation: in the
interface, I may have something like this:
public abstract class SmartQueue {
public event DataAvailabilityEventHandler DataArrived;
}
Now, consider what I have to do in MemorySmartQueue:
public class MemorySmartQueue : SmartQueue {
public override void poolForData() {
// data available!
if(DataArrived == null) {
// BZZZZTTT!!! Can only do this in SmartQueue!
DataArrived(this, args);
}
}
}
I can't believe this. Either I'm missing a really obvious thing, or I
have to deal with this... this... awkward mechanism. Why the hell
doesn't it allow me to raise an event in the derived class? I don't want
to put any behaviour in my abstract class, I want to put just an interface!
Is there any way around this? (Plus I hope Whidbey will give us a Set
collection, dammit).
TIA!
Elder
A class of mine needs to tell the outside world when its buffer is not
empty. The problem is that C# seems to force you to put the
event-raising code in the base class. To illustrate, consider what I'll
do in Java:
public interface DataAvailabilityListener extends java.util.EventListener {
void dataArrived(DataAvailabilityEvent event);
}
then, in my base class, I can do this:
public abstract class SmartQueue {
addDataAvailabilityListener(DataAvailabilityListener listener);
}
then in my implementation class, say a memory-backed smart queue, I can
do this:
public class MemorySmartQueue implements SmartQueue {
public void poolForData() {
// data available!
while(iter.hasNext()) {
DataAvailabilityListener lis =
(DataAvailabilityListener)iter.next();
lis.dataArrived(someEvent);
}
}
}
Simple and straighforward. However, consider a C# implementation: in the
interface, I may have something like this:
public abstract class SmartQueue {
public event DataAvailabilityEventHandler DataArrived;
}
Now, consider what I have to do in MemorySmartQueue:
public class MemorySmartQueue : SmartQueue {
public override void poolForData() {
// data available!
if(DataArrived == null) {
// BZZZZTTT!!! Can only do this in SmartQueue!
DataArrived(this, args);
}
}
}
I can't believe this. Either I'm missing a really obvious thing, or I
have to deal with this... this... awkward mechanism. Why the hell
doesn't it allow me to raise an event in the derived class? I don't want
to put any behaviour in my abstract class, I want to put just an interface!
Is there any way around this? (Plus I hope Whidbey will give us a Set
collection, dammit).
TIA!
Elder